How to compile additional source files in cmake after the build process - c

I have a project in cmake for windows which contains a Pro*C source file called database.proc, my goal is to generate a C source file from the .proc file and add it to the project to be linked along the other source files, I've tried to add a custom command to achieve this without success
add_custom_command(TARGET myproj OUTPUT PRE_LINK
COMMAND ${PROC} iname=${PROJECT_SOURCE_DIR}/connection.proc SQLCHECK=SYNTAX
MODE=ANSI IRECLEN=255 ORECLEN=255
ONAME=${PROJECT_SOURCE_DIR}/connection.c
COMMAND ${CMAKE_C_COMPILER} ${CMAKE_C_FLAGS}
${PROJECT_SOURCE_DIR}/connection.c )
Is there some way to do this?

I'm not familiar with Pro*C, but it looks like you're mixing together the two different versions of add_custom_command.
The first version add_custom_command(OUTPUT ...) is used to generate a file which is then added as a dependency of another CMake target. When that target is built, the custom command is executed first in order to generate the output file.
The second version add_custom_command(TARGET ...) is used to define a pre-build, pre-link or post-build command; one which does not necessarily create a file, but which executes in conjunction with building the associated target.
If you only have one target which depends on the output of Pro*C, then the first version is probably your best bet:
add_custom_command(OUTPUT ${PROJECT_SOURCE_DIR}/connection.c
COMMAND ${PROC} iname=${PROJECT_SOURCE_DIR}/connection.proc SQLCHECK=SYNTAX
MODE=ANSI IRECLEN=255 ORECLEN=255
ONAME=${PROJECT_SOURCE_DIR}/connection.c)
add_executable(myproj ${PROJECT_SOURCE_DIR}/connection.c <other sources>)

Related

Converting .o file into static library in linux and make it usable in terminal

I want to create a simple library and after compilation and ar command I can get resulting .a file.
Now I want to add this file as a static library and use it in terminal, I dont know if its possible. but idea is from other library named Ctypes.sh on github. that library can be used to make syscalls from terminal or bash terminal.
I like to know how I can add my mylib.a as static libary and make it usable from terminal.
the library is simple I just want to invoke a few syscalls in linux from terminal.
I also looked into the code of ctypes.sh so my library is also be used to make some syscalls.
the reference I used above is here
https://github.com/taviso/ctypes.sh/wiki
Every command that you run on linux are binary file that you execute.
When you run a command like:
ls -a
it's like running:
./ls -a
Where the ./ls is the binary and -a a parameter.
All the binary used in a terminale is stocked in the bin (included in the default PATH). When you run a command your terminal will check in first, in the folder to find the binary and after, he gonna check every folder in the PATH environement variable. If you wan't to add a specific folder to the PATH to use a personnal folder for different baniry (check this link).
In your probleme you have a library with different function (I suppose) that you wan't to use in a terminale. Have 2 solution:
Split your library in multiple micro programme, that you can execute in the terminale,
Create a programme whit param to run different function.

add dependency on cmake's built-in target

Let's suppose we want to build a simple library, with one header file and one source file (my_lib.c and my_lib.h, respectively).
The CMakeLists.txt will be something like:
cmake_minimum_required(VERSION 3.14)
project(example C)
add_library(example my_lib.c my_lib.h)
the available targets are (output from make help):
my_lib.i
...
Now, suppose we have a script (let's say my_script.py) that requires the preprocessed output of my_lib.c (the output of target my_lib.i) as an input parameter.
So, we add the following code to our CMakeLists.txt:
add_custom_target(my_script
DEPENDS
COMMAND python my_scipt.py path/to/my_lib.i)
The question is: is it possible to add a dependency (DEPENDS parameter of add_custom_target above) in order to build target my_lib.i?
I have read similar questions, but all propose to invoke the preprocessor explicitly, which will probably break the build by modifying the compiler.
Thanks

CMake - always build specific file

I have a specific file that should be rebuilt on each compilation regardless if it has been modified or not. The reason is that it is depending on system macros whose values change. How could I force rebuild with CMake? I'd like to not bind it to specific target: the file should be "touched" before any of the targets specified in CMakeLists.txt begins the actual build process.
CMake has a add_custom_target command:
Adds a target with the given name that executes the given commands. The target has no output file and is always considered out of date even if the commands try to create a file with the name of the target. [...] By default nothing depends on the custom target. Use the add_dependencies() command to add dependencies to or from other targets.

CMake - Getting list of source/header files for various subprojects

Background
I have a large cmake project that makes use of dozens of subprojects: some from in-house code bases, and some third-party projects which also use CMake.
To ensure common compiler options, I setup a macro in CMake called CreateDevFlags which is run in only the in-house sub-projects own CMakeLists file as the first line of code to execute. This makes sure that I don't break the compiler flags, output directory overrides, etc, for third-party projects, and all of the code I wrote myself is built with identical options.
Additionally, each sub project has a simple block of code along the lines of the following to define the source files to be compiled:
file(GLOB subproject_1A_SRC
"src/*.c"
)
file(GLOB subproject_1A_INC
"inc/*.h"
)
file(GLOB subproject_2B_SRC
"src/*.c"
"extra_src/*.c"
)
file(GLOB subproject_2B_INC
"inc/*.h"
"extra_details_inc/*.h"
)
Goal
I would like to add a sanity-check custom rule/function to the "master" CMakeLists file at the project root which runs all of the code for in-house subprojects through a code sanitizer (checks newlines, enforces style rules, etc).
Question
Is there a trivial way to have all "special" (ie: in-house) subprojects append their own source files to a "master" list of source (.c) and header (.h) files (possibly via the macro I created)? I realize I could manually create this list in the master CMakeLists file, but then I'd be duplicating efforts, and code maintainers would have to modify code in two places with this in effect.
Thank you.
One possible implementation would be to have a list called FILE_TRACKER defined at top scope for your project. Then, you could do something like
# Create local list to append to
set(LOCAL_LIST ${FILE_TRACKER})
# Append all of your source files, from your local source
foreach(SRC_FILE ${subproject_1A_SRC})
list(APPEND LOCAL_LIST ${SRC_FILE})
endforeach()
# Append to the upper macro (note: was initially set with FILE_TRACKER)
set(FILE_TRACKER ${LOCAL_LIST} PARENT_SCOPE)
The developers would only have to add their source to the one list, and the macro at the top level will be updated with the files.
In the end. the following approach solved my problem:
set(DIR1_SRCS "file1.cpp" PARENT_SCOPE)
and then in ./CMakeLists.txt
set(SRCS ${DIR1_SRCS} ${DIR2_SRCS})
I suggest you don't examine header files. Instead use include dirs for the paths to the header files. If you do this you will automatically get the depends working without having to track them yourself.
Your sanitizer should be able to parse the actual code to find and read the included headers.

add_custom_command from another target

I'm currently trying to setup a CMake project with two executables, one of which is a simple utility used to generate code for the other. Relevant bits of CMakeLists.txt:
add_executable(lua2c lua2c.c)
add_custom_command(OUTPUT lcode.c COMMAND lua2c lcode.lua lcode.c MAIN_DEPENDENCY lua2c)
...
add_executable(darpem ... lcode.c)
With this setup, target lua2c winds up with no dependencies, which causes cc to complain about no input files. If I remove the add_custom_command line, then lua2c is built properly, but obviously doesn't generate the file lcode.c. Is this possible in CMake? Would I need to add a subdirectory dependency of sorts?
Using CMake version 2.8.1 on Ubuntu 13.04, x86-64.
NOTE: For my particular case, because lua2c is simple enough, I can use a different language. I am, however, still curious as to how this might be possible (for more complex setups).
From the documentation :
Note that MAIN_DEPENDENCY is completely optional and is used as a suggestion to visual studio about where to hang the custom command.
Maybe this should solve your problem :
add_executable(lua2c lua2c.c)
add_custom_command(OUTPUT lcode.c COMMAND lua2c lcode.lua lcode.c DEPENDS lua2c)
# ^^^^^^^
...
add_executable(darpem ... lcode.c)
Or if it doesn't work, this one should work :
add_executable(lua2c lua2c.c)
add_custom_command(TARGET lua2c
POST_BUILD
COMMAND lua2c lcode.lua lcode.c )
...
add_executable(darpem ... lcode.c)
add_dependencies( darpem lua2c )
It simply add a post build event after the build of lua2c. And it add lua2c as a dependency of darpem.

Resources