Cmake build in a specific order

I am trying to create a JNI jar with CMake. To do this, do the following:

  • compile .class files
  • create .h headers
  • create your own library
  • jar all

Where

  • executed using add_jar() (I prefer that in custom_command)
  • executed using add_custom_command(TARGET ...)
  • executed using add_library()
  • executed using add_custom_command(TARGET ...) (since the -C option is not supported by add_jar)

How can I ensure the correct order? Sometimes I get errors on the first start.

add_custom_command has a POST / PRE parameter, but add_jar and add_library do not. add_custom_command , which has no TARGET argument, has a DEPENDS option, should this be used?

Is there any way to tell add_library wait for the 2. user command to complete?

+4
source share
1 answer

I assume the error is that you are calling add_library with source files that do not already exist during the first run of CMake?

If so, you can set the GENERATED property in these source files using set_source_files_properties . This allows CMake to know that it is normal if these files do not exist during configure (when CMake starts), but they will exist during build.

For the add_jar command add_jar run before add_library , create a dependency on the add_jar target using add_dependencies . For the add_custom_command command add_custom_command run before add_library , use the custom TARGET ... PRE_BUILD .

For example, if the source list for lib is stored in a variable named ${Srcs} , you can do:

 # Allow 'Srcs' to not exist at configure-time set_source_files_properties(${Srcs} PROPERTIES GENERATED TRUE) add_library(MyLib ${Srcs}) # compile .class files add_jar(MyJarTarget ...) # generate .h headers add_custom_command(TARGET MyLib PRE_BUILD COMMAND ...) # Force 'add_jar' to be built before 'MyLib' add_dependencies(MyLib MyJarTarget) 
+8
source

All Articles