I'm trying to create a JNI jar with CMake. For that the following has to be done in the appropriate order:
where
add_jar()
(I prefered that at custom_command)add_custom_command(TARGET ...)
add_library()
add_custom_command(TARGET ...)
(because -C option is not supported by add_jar)How can I ensure that the proper order is followed? I get errors sometimes on the first run.
add_custom_command
has a POST
/PRE
build option, but add_jar
and add_library
does not. The add_custom_command
that does not have the argument TARGET
has the DEPENDS
option, should I use that?
Is there a way of telling add_library
to wait for the 2. custom command to have been ran?
I guess the error is that you're calling add_library
with source files which don't yet exist during the first run of CMake?
If so, you can set the GENERATED
property on those source files using the set_source_files_properties
command. This lets CMake know that it's OK for those files to not exist at configure-time (when CMake runs), but that they will exist at build-time.
To ensure that the add_jar
command executes before add_library
, create a dependency on the add_jar
target using add_dependencies
. To ensure that the add_custom_command
command executes before add_library
, have the custom command use the TARGET ... PRE_BUILD
options.
For example, if your list of sources for the lib is held in a variable called ${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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With