M
M
Max Goncharenko2018-10-31 14:12:55
C++ / C#
Max Goncharenko, 2018-10-31 14:12:55

Is it possible through CMake to build a project that is going to be built with a bash script?

I'm trying to add a specific version of openssl to my CMake tree.
I know that it is possible to execute commands in the shell via execute_command which works at configuration time and with add_custom_command which works at build time. Unfortunately, in the second option, you either need to specify a target or an output file - I don’t have a target, what OUTPUT is, I didn’t fully understand.
If someone has experience with adding non-CMake projects to CMake, please share.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
4
4rtzel, 2018-10-31
@reverse_kacejot

add_custom_command is useful when you have some of your sources generated by some other program (eg flex, protobuf). In this case, OUTPUT will point to the generated files using the COMMAND command. You can see my answer on the topic .
The easiest way (but not always the best) to build a third party non-CMake project is to use ExternalProject_Add . Difficulties arise when you need to add this project as a dependency to your own. The most obvious way is to use find_package with the correct paths. For example for OpenSSL:

...
# Вместо этой строчки также можно указать путь из командой строки:
# $ cmake -DOPENSSL_ROOT_DIR=/usr/local/ssl -DOPENSSL_LIBRARIES=/usr/local/ssl/lib ..
set(CMAKE_MODULE_PATH  ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/openssl)
# Используем FindOpenSSL.cmake для поиска
find_package(OpenSSL REQUIRED)
...

The problem with this method is that ExternalProject_Add is done at the build step (call to make) and not at the configuration step (call to cmake), find_package will fail if there is no already built version of openssl somewhere in ${CMAKE_CURRENT_SOURCE_DIR}/openssl.
Another option for adding a dependency is to use a combination of target_link_directories and target_link_libraries . Example:
target_link_directories(my_target ${CMAKE_CURRENT_SOURCE_DIR}/openssl/lib)
target_link_libraries(my_target PRIVATE crypto ssl)

The problem with this method is that we are tied to a specific path and library names (although we should only depend on targets).
In many projects, such problems are solved by the so-called SuperBuild. This is when you have a main CMakeLists.txt file in which all other projects (including yours) are defined as ExternalProject. Then it will be possible to build dependencies between them normally and calls like find_package will work, but that's another story.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question