Also, you can generate the configuration files for your build system in the generation process. We designed it to allow a several levels of generator-generated dependencies so that one generator can rely on generated products of another.Īnother two tricks to consider, if your build system allows you to use regular expressions to build files, you may want to use it. We also ended up running the build system twice with two different graphs, one graph for generators and another graph for the other files. (we don't need it at the moment as our files don't change that often) The build system of course did not run the generator directly, but instead we wrote a small python script that decided how to run the generator properly - for example an improvement we can add is generating all the files to /tmp and only moving the changed files (comparing using diff), thus only the changed files will be touched. If the generator or the xml files changed the build system ran the generator (which generated everything - even if only one file changed, that means it touches all the generated files). Our solution involved treating the xmls and the generator as code that needs to compile, treating the generator as a dependency and the xml files as source. I don't know cmake, but this might give you several ideas: Only we don't work with cmake but another build system. If you do not know the name of the files, you can use ADD_CUSTOM_COMMAND( OUTPUT generated.timestampĬOMMAND $" COPY_ONLY )įILE( GLOB GENERATED_SOURCES. The custom command will take care of regeneration IF required. However, since it does nothing (besides printing the comment and checking the dependencies) in this case, that doesn't matter. NB: The RunGenerator target will always be considered out-of-date and, thus, always run. Then make your other targets depend on this one: ADD_DEPENDENCIES( MyTarget RunGenerator ) To ensure this, the following should do the trick: ADD_CUSTOM_TARGET( RunGenerator DEPENDS generatedfile1 generatedfile2ĬOMMENT "Checking if re-generation is required" ) Make sure that the generated files are not used in more than one independent target that may compile in parallel or you may(will) get a conflict during your build. ADD_CUSTOM_COMMAND( OUTPUT generatedfile1 generatedfile2ĬOMMAND python generateSources.py xmlfile1 xmlfile2ĭEPENDS xmlfile1 xmlfile2 generateSources.pyĬOMMENT "Generating source code from XML" ) ![]() It allows you to define input and output dependencies and will only run if the outputs are older than the inputs. Defining such properties globally in the top-level CMakeLists.txt file prevents scenarios where public headers of a dependent target causing a depending target not to compile because the depending target uses stricter compiler options. Use ADD_CUSTOM_COMMAND to trigger your generator. For example, a project might use a common set of compiler warnings. My code generator will generate a variable number of files. But this relies on the output of the code generator being known in advance. When I changed the template file (DataTableSchema.h) and try build, ninja only executes custom command and not build a library. What is the proper way to handle such a situation in cmake? I have seen this previous answer: " Get CMake to execute a target in project before building a library". Code os:windows, gen:ninja hihig2001 (JihwanKim) October 6, 2022, 4:05pm 1 Hi, I’m trying to generate a header file during build. In other words, I want the libraries to depend on the generator to have run. I also want to make sure that the generated code is always run before my libraries. However, this runs every time I run cmake and it touches the files, causing my generated code to be recompiled and adding to my total compile time. At the moment, I am using execute_process to fire off the generator. I want to use cmake to manage the build process. The input (an XML file) to the generator changes.I know that the generated code will only change if one of two conditions are true: ![]() ![]() This code is subsequently compiled and linked to my libraries and executables. General advice: manually copying files from the build tree to either elsewhere in the build tree or especially to the source tree is practically never needed and a bad idea.I am working on a source code repository that generates some C++ code by running a python script outputting headers and implementation. Target_link_libraries(target5 PRIVATE target4objs) Target_link_libraries(target4 PRIVATE target4objs)Īnd in subdir5/6 you would write: add_executable(target5 src1.cpp. )Īdd_executable(target4 extra_src_not_shared.cpp. Then in subdir4's CMakeLists.txt you would write: add_library(target4objs OBJECT src1.cpp src2.cpp. Suppose you have a higher-level CMakeLists.txt file with. See below for a sketch and the documentation here: ![]() You can achieve this with object libraries in CMake. I have multiple sub directories the 4th one has add_executable which generates object files and exe file the fifth and sixth sub directories need some object files.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |