EDIT: I have found a probable cause but I do not understand why: the last line in the below script Project(Externals) when removed fixes my issue. So the question now why??
cmake_minimum_required(VERSION 2.8)
include(ExternalProject)
MACRO(EXTERNAL_DEF aNewTargetName aPathToSource)
ExternalProject_Add(
${aNewTargetName}
PREFIX ${CMAKE_INSTALL_PREFIX}
SOURCE_DIR ${aPathToSource}
TMP_DIR "${CMAKE_INSTALL_PREFIX}/tmp/${CMAKE_BUILD_TYPE}"
DOWNLOAD_DIR "${CMAKE_INSTALL_PREFIX}/src/${CMAKE_BUILD_TYPE}"
BINARY_DIR "${CMAKE_INSTALL_PREFIX}/src/${CMAKE_BUILD_TYPE}/${aNewTargetName}-build"
STAMP_DIR "${CMAKE_INSTALL_PREFIX}/src/${CMAKE_BUILD_TYPE}/${aNewTargetName}-stamp"
CMAKE_ARGS
-DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
-DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}
--debug-output
BUILD_COMMAND "${CMAKE_COMMAND}" --build
"${CMAKE_INSTALL_PREFIX}/src/${CMAKE_BUILD_TYPE}/${aNewTargetName}-build" --config "${CMAKE_BUILD_TYPE}"
#INSTALL_DIR "${CMAKE_INSTALL_PREFIX}"
)
ENDMACRO()
get_filename_component(zlibAbsPath "./zlib" ABSOLUTE)
EXTERNAL_DEF(zlib_external ${zlibAbsPath})
Project(Externals)
I invoke cmake on the above CMakeLists.txt file with CMAKE_INSTALL_PREFIX set to let's say "d:\externals" and CMAKE_BUILD_TYPE set to "Release"
Expectation:
I would expect only the Release configuration to get built. And after it gets built I would expect it to get installed in D:\externals\bin\zlib.dll.
Problem:
In reality, ExternalProject_Add builds both a Debug and a Release, and installs the debug version of the dll in D:\externals\bin\zlibd.dll
Isn't my build script correct? What am I doing wrong?
EDIT:
some more info. I just noticed. In the generated D:\externals\src\Release\zlib_external-build\zlib.sln, the INSTALL target is not selected to build at all. If I check it to build for the Release configuration then hit "Build" from visual studio, the INSTALL target builds and installs the files where I expect them. I have no idea what's going on...
CMAKE_BUILD_TYPE only work with single-configuration projects
you can change to:
BUILD_COMMAND ""
INSTALL_COMMAND
${CMAKE_COMMAND}
--build .
--target install
--config Release
BUILD_ALWAYS 1
Use DCMAKE_CFG_INTDIR variable.
...
CMAKE_ARGS
...
-DCMAKE_CFG_INTDIR=Release
Related
I am having an issue with CMakes Add_External_Project functionality (more of an annoyance than anything else). Specifically, I do not understand the keys CONFIGURE_COMMAND, BUILD_COMMAND and INSTALL_COMMAND.
In the following (working) example, which downloads Google's test library, the two files at the end of the question will ensure that the third party libraries are downloaded and built (not installed).
However, when I tried to add configure and build commands as "CONFIGURE_COMMAND" and "BUILD_COMMAND" (cmake . and cmake --build) instead of having to do execute_process CMake craps out with the error message:
[ 55%] Performing configure step for 'googletest'
/bin/sh: 1: cmake .: not found
Am I trying to do something that is obviously not within the scope of the Add_External_Project functionality?
Example Files:
CMakeLists.txt
cmake_minimum_required (VERSION 3.0)
project (Test VERSION 0.1.0.0 LANGUAGES CXX)
# Download and unpack googletest at configure time
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt.in" "${CMAKE_BINARY_DIR}/googletest-download/CMakeLists.txt" #ONLY)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/googletest-download" )
execute_process(COMMAND ${CMAKE_COMMAND} --build . WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/googletest-download")
add_subdirectory("${CMAKE_BINARY_DIR}/googletest-src" "${CMAKE_BINARY_DIR}/googletest-build")
CMakeLists.txt.in
cmake_minimum_required(VERSION 3.0)
project(third-party NONE)
include(ExternalProject)
ExternalProject_Add(googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG master
SOURCE_DIR "#CMAKE_BINARY_DIR#/googletest-src"
BINARY_DIR "#CMAKE_BINARY_DIR#/googletest-build"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)
If you don't specify CONFIGURE_COMMAND at all, it will assume a CMake project and run the appropriate cmake command for you (by appropriate, I mean it will use the same CMake generator as your main build, etc.). Similarly, if you leave out BUILD_COMMAND, it will also assume a CMake project and do cmake --build for you. So in your case, just leave out those two lines and ExternalProject_Add() should do exactly what you want.
The main reason you might specify these two options as empty strings is to prevent those steps from doing anything at all. This can be useful, for example, to use ExternalProject_Add() simply for its download and unpacking functionality. This exact situation is used in a technique described here for downloading the source of GoogleTest so it can be added to your project via add_subdirectory(), making it part of your build (see also this answer and other answers to that question for some related material). I suspect this might be where your code is derived from, as the structure looks similar.
For completeness, if you find yourself in a situation where you do need to specify a CMake command, don't use a bare cmake to refer to the command to run. Instead, always use ${CMAKE_COMMAND}, which is provided by CMake as the location of the CMake executable currently being used to process the file. Using this variable means cmake doesn't have to be on the user's PATH and also ensures that if the developer chooses to run a different version of CMake other than the one on the PATH, that same cmake will still be used for the command you are adding.
You can use PATCH_COMMAND like this:
option(WITH_MBEDTLS "Build with mbedtls" OFF)
if(WITH_MBEDTLS)
ExternalProject_Add(external-mbedtls
URL https://github.com/ARMmbed/mbedtls/archive/mbedtls-2.16.1.tar.gz
UPDATE_COMMAND ""
PATCH_COMMAND ./scripts/config.pl set MBEDTLS_THREADING_C &&
./scripts/config.pl set MBEDTLS_THREADING_PTHREAD
CMAKE_ARGS
-DCMAKE_INSTALL_PREFIX:PATH=${PROJECT_BINARY_DIR}/third_party/mbedtls
-DCMAKE_TOOLCHAIN_FILE:PATH=${TOOLCHAIN_FILE}
-DCMAKE_BUILD_TYPE:STRING=Debug
-DENABLE_TESTING:BOOL=OFF
-DENABLE_PROGRAMS:BOOL=ON
TEST_COMMAND ""
)
set(MBEDTLS_PREFIX ${PROJECT_BINARY_DIR}/third_party/mbedtls PARENT_SCOPE)
endif(WITH_MBEDTLS)
I'm building dependency project with cmake ExternalProject_Add command:
include(ExternalProject)
...
set(COMMON_BASE_PROJECT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../CommonBase)
ExternalProject_Add(CommonBaseProject
SOURCE_DIR ${COMMON_BASE_PROJECT_DIR}
BINARY_DIR ${COMMON_BASE_PROJECT_DIR}/build
INSTALL_COMMMAND ""
)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
include_directories(${COMMON_BASE_PROJECT_DIR}/include)
add_library(
${LIBRARY_NAME}
SHARED
${SRC_FILES}
${INCLUDE_FILES}
)
target_link_libraries (Bios ${COMMON_BASE_PROJECT_DIR}/build/libCommonBase.dll)
add_dependencies(Bios CommonBaseProject)
but i get error:
[100%] Linking CXX shared library libCommonBase.dll
[100%] Built target CommonBase
[ 50%] Performing install step for 'CommonBaseProject'
make[3]: *** No rule to make target 'install'. Stop.
I don't need to make install step, so my question is: how to disable it?
You almost had it: Instead of INSTALL_COMMAND "" put something like
INSTALL_COMMAND cmake -E echo "Skipping install step."
You can generate a target for the build step with STEP_TARGETS build and add dependency on this particular target. The step targets are named <external-project-name>-<step-name> so in this case the target representing the build step will be named CommonBaseProject-build.
You probably also want to exclude the CommonBaseProject from the "all" target with EXCLUDE_FROM_ALL TRUE.
ExternalProject_Add(CommonBaseProject
SOURCE_DIR ${COMMON_BASE_PROJECT_DIR}
BINARY_DIR ${COMMON_BASE_PROJECT_DIR}/build
STEP_TARGETS build
EXCLUDE_FROM_ALL TRUE
)
add_dependencies(Bios CommonBaseProject-build)
Not relevant to your question, which it was already answered, but in my case I had the following ExternalProject_Add directive:
ExternalProject_Add(external_project
# [...]
# Override build/install command
BUILD_COMMAND ""
INSTALL_COMMAND
"${CMAKE_COMMAND}"
--build .
--target INSTALL # Wrong casing for "install" target
--config ${CMAKE_BUILD_TYPE}
)
In this case cmake quits with very similar error (*** No rule to make target 'INSTALL'), but in this case it's the external project that is looking for incorrect uppercase INSTALL target: correct case is install instead. Apparently, that worked in Windows with MSVC but fails in unix operating systems.
Since at least CMake 3.10 the empty string is sufficient to suppress the install step:
Passing an empty string as the <cmd> makes the install step do nothing.
The same goes for the other stages; see the docs for more.
If you're still building with CMake <3.10 then you need to update CMake ;)
I have a project with a CMakeLists.txt files in the root and the project compiles fine on Linux and OSX. Now I want to cross compile it for MIPS OpenWRT.
I would like to automate it as much as possible, so I would use following code to download the toolchain and set the compiler variables:
ExternalProject_Add(ar71xx-toolchain
PREFIX "${PROJECT_BINARY_DIR}/external/openwrt"
URL "http://downloads.openwrt.org/barrier_breaker/14.07/ar71xx/generic/OpenWrt-Toolchain-ar71xx-for-mips_34kc-gcc-4.8-linaro_uClibc-0.9.33.2.tar.bz2"
UPDATE_COMMAND ""
PATCH_COMMAND ""
BUILD_COMMAND ""
CONFIGURE_COMMAND ""
INSTALL_COMMAND ""
)
ExternalProject_Get_Property(ar71xx-toolchain SOURCE_DIR)
SET(CMAKE_C_COMPILER ${SOURCE_DIR}/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2/bin/mips-openwrt-linux-gcc)
SET(CMAKE_CXX_COMPILER ${SOURCE_DIR}/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2/bin/mips-openwrt-linux-g++)
SET(CMAKE_STRIP ${SOURCE_DIR}/toolchain-mips_34kc_gcc-4.8-linaro_uClibc-0.9.33.2/bin/mips-openwrt-linux-strip)
I thought that I can put it in a separate toolchain file and pass it with -DCMAKE_TOOLCHAIN_FILE, but it seems that ExternalProject_Add is not executed inside the toolchain file.
I would like to avoid putting the toolchain download step into the main CMakeLists.txt since it's actually not essential for the project itself and would require doing the same for each target platform...
So is there a way to define optional steps for a current cross compile build and pass it somehow as command line parameter to be executed before the main project build?
UPDATE:
Based on Tsyvarev's answer that works for me in the toolchain file:
set(CMAKE_SYSTEM_NAME Linux)
set(TOOLCHAIN_DIR ${PROJECT_BINARY_DIR}/external/openwrt/toolchain)
if(NOT EXISTS ${TOOLCHAIN_DIR})
file(DOWNLOAD http://downloads.openwrt.org/barrier_breaker/14.07/ar71xx/generic/OpenWrt-Toolchain-ar71xx-for-mips_34kc-gcc-4.8-linaro_uClibc-0.9.33.2.tar.bz2 ${TOOLCHAIN_DIR}/toolchain.tar.bz2 SHOW_PROGRESS)
execute_process(COMMAND tar --strip-components=2 -xjf ${TOOLCHAIN_DIR}/toolchain.tar.bz2 WORKING_DIRECTORY ${TOOLCHAIN_DIR})
execute_process(COMMAND rm ${TOOLCHAIN_DIR}/toolchain.tar.bz2)
endif()
SET(CMAKE_C_COMPILER ${TOOLCHAIN_DIR}/bin/mips-openwrt-linux-gcc)
SET(CMAKE_CXX_COMPILER ${TOOLCHAIN_DIR}/bin/mips-openwrt-linux-g++)
SET(CMAKE_STRIP ${TOOLCHAIN_DIR}/bin/mips-openwrt-linux-strip)
SET(CMAKE_FIND_ROOT_PATH ${TOOLCHAIN_DIR})
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
There is one issue when passing -DCMAKE_TOOLCHAIN_FILE as CMAKE parameter to other projects added with ExternalProject_Add. Because of it's own ${PROJECT_BINARY_DIR} it will download the toolchain again. But this is another problem...
ExternalProject_add executes all steps at build time, not at configuration time.
For download file you can use file(DOWNLOAD ...) command. For extract files from archive just use execute_process with appropriate command.
I use the following CMake file to automatically download and compile the Boost libraries. After generating project files for my compiler, I run cmake --build . --config Debug and cmake --build . --config Release to build both variants. So I envoke the build process twice. Even though CMake is clever enough to not download the archive again if the checksum still matches, it extracts it into the source directory twice. Extraction takes a couple of minutes for the large Boost libraries, so I'd like to prevent CMake from doing it for the second build.
ExternalProject_Add(Boost
PREFIX ${BOOST_PREFIX}
TMP_DIR ${BOOST_PREFIX}/temp
STAMP_DIR ${BOOST_PREFIX}/stamp
#--Download step--------------
DOWNLOAD_DIR ${BOOST_PREFIX}/download
URL http://downloads.sourceforge.net/project/boost/boost/1.56.0/boost_1_56_0.tar.gz
URL_MD5 8c54705c424513fa2be0042696a3a162
#--Update/Patch step----------
UPDATE_COMMAND ""
#--Configure step-------------
SOURCE_DIR ${BOOST_PREFIX}/source
CONFIGURE_COMMAND ${BOOST_CONFIGURE_COMMAND}
#--Build step-----------------
BUILD_COMMAND ${BOOST_BUILD_EXECUTABLE} install
--build-dir=${BOOST_PREFIX}/build
--prefix=${BOOST_PREFIX}/install
variant=${BOOST_VARIANT}
link=${BOOST_LINK}
threading=multi
address-model=32
toolset=${BOOST_TOOLSET}
runtime-link=${BOOST_RUNTIME_LINK}
BUILD_IN_SOURCE 1
#--Install step---------------
INSTALL_COMMAND ""
)
In this particually example it would possible to just use variant=debug,release, but there are some other external dependencies in my project that I build this way. How can I make CMake only extract the archive if it downloaded a new one?
Create two projects: Foo-Release and Foo-Debug. Leave DOWNLOAD_COMMAND empty for the second project and make it depends on the first. Set SOURCE_DIR explicilty for both projects (point to the same directory). I.e.:
set(source_dir "${CMAKE_CURRENT_BINARY_DIR}/3rdParty/Foo/Source")
ExternalProject_Add(
Foo-Release
URL "..."
URL_HASH SHA1=...
SOURCE_DIR "${source_dir}"
...
)
ExternalProject_Add(
Foo-Debug
DOWNLOAD_COMMAND
""
SOURCE_DIR "${source_dir}"
...
)
add_dependencies(Foo-Debug Foo-Release)
Example
I have an application with a mildly complicated build process, and as a bit of a newb to CMake, I was wondering if anyone could provide me with any pointers.
At preset, the application consists of a single executable, built from a source tree provided in the src and include folders.
It requires a few libraries to work, the big ones being Boost and Python. Python is embedded in the application, and Boost requires knowledge of the custom python install at compile time. I also use Qt, but I'm just linking against the system Qt for this.
What I'd like to have at the end is a stage folder, containing the compiled executable, and a lib folder with the required boost and python libraries.
At present, I have a single CMakeLists.txt file, and I am using ExternalProject to build Boost and Python from bzipped tarballs of their source. It gets a little messy where I copy out the compiled libs from the prefixed install directories.
Things are working, but I have a feeling I'm doing things very backwards. I sometimes see multiple CMakeLists in nested subdirectories but don't know how they would relate to my project. Would anyone who has worked on similarly scoped projects be able to weigh in and give me some pointers?
I should add that I hope to include Windows as a platform in the near future, and that things are currently running on Linux.
Note: This is my current CMakeLists.txt, I realise that boost isn't configured and that things aren't fully moved to the stage folder. I have been doing this manually, but I wanted to ask before I dig myself much deeper :)
Thanks!
CmakeList.txt
cmake_minimum_required(VERSION 2.6)
set(CMAKE_BUILD_TYPE Debug)
set(PROJ_NAME "mwave")
project(${PROJ_NAME})
include_directories("include")
include(ExternalProject)
# Add cmake dir to cmake module path so custom find modules will work
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH})
#Build Python via External Project
ExternalProject_Add(
Python
PREFIX ${CMAKE_CURRENT_BINARY_DIR}/external/python
URL ${CMAKE_CURRENT_SOURCE_DIR}/extern/Python-3.3.0.tar.bz2
URL_MD5 2dbff60afed2b5f66adf6f77dac9e139
UPDATE_COMMAND ""
CONFIGURE_COMMAND ./configure -q --prefix=${CMAKE_CURRENT_BINARY_DIR}/external/python --enable-shared
BUILD_COMMAND make
BUILD_IN_SOURCE 1
INSTALL_COMMAND make install
)
# Manually copy the compiled python files and dirs to our stage folder
add_custom_command(TARGET Python PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
${CMAKE_CURRENT_BINARY_DIR}/external/python/lib/pkgconfig
${CMAKE_CURRENT_BINARY_DIR}/stage/lib/pkgconfig)
add_custom_command(TARGET Python PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
${CMAKE_CURRENT_BINARY_DIR}/external/python/lib/python3.3
${CMAKE_CURRENT_BINARY_DIR}/stage/lib/python3.3)
add_custom_command(TARGET Python PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_CURRENT_BINARY_DIR}/external/python/lib/libpython3.so
${CMAKE_CURRENT_BINARY_DIR}/stage/lib/libpython3.so)
add_custom_command(TARGET Python PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_CURRENT_BINARY_DIR}/external/python/lib/libpython3.3m.so.1.0
${CMAKE_CURRENT_BINARY_DIR}/stage/lib/libpython3.3m.so.1.0)
add_custom_command(TARGET Python PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E create_symlink
libpython3.3m.so.1.0
${CMAKE_CURRENT_BINARY_DIR}/stage/lib/libpython3.3m.so)
#Python
set(PYTHON_INCLUDE_DIRS "${CMAKE_CURRENT_BINARY_DIR}/external/python/include/python3.3m")
set(PYTHON_LIBRARIES "${CMAKE_CURRENT_BINARY_DIR}/external/python/lib/libpython3.3m.so" "pthread" "m" "util" "readline")
#Build boost via External Project
ExternalProject_Add(
Boost
DEPENDS Python
PREFIX ${CMAKE_CURRENT_BINARY_DIR}/external/boost
URL ${CMAKE_CURRENT_SOURCE_DIR}/extern/boost_1_51_0_mwave.tar.bz2
URL_MD5 fe203a243e451b4dd4754c7b283b1db9
UPDATE_COMMAND ./bootstrap.sh --with-libraries=python,system,thread,program_options
CONFIGURE_COMMAND ""
BUILD_COMMAND ./b2
BUILD_IN_SOURCE 1
INSTALL_COMMAND ""
)
#Boost (workaround until external project is working)
set(Boost_INCLUDE_DIRS "/opt/mwave/include")
set(Boost_LIBRARIES "/opt/mwave/lib/libboost_python3.so" "/opt/mwave/lib/libboost_program_options.so")
#OpenImageIO
set(OIIO_PATH "/opt/mwave/oiio/dist/linux64.debug")
find_package(OIIO REQUIRED)
#Qt4
find_package(Qt4 REQUIRED)
set(QT_USE_QTOPENGL TRUE)
include(${QT_USE_FILE})
add_definitions(${QT_DEFINITIONS})
#OpenGL
find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)
# Mwave app
set(HEADERS
"include/Application.h"
"include/ImageChannel.h"
"include/CompDag.h"
"include/Dag.h"
"include/Gui/DagView.h"
"include/Gui/DagScene.h"
"include/Gui/MainWindow.h"
"include/Gui/GLViewer.h"
"include/Gui/Nodes/GNodeEdge.h"
"include/Gui/Nodes/GNodeLabel.h"
"include/Gui/Nodes/GNodeCacheStatus.h"
"include/Gui/Nodes/GNode.h"
"include/Gui/Nodes/GRead.h"
"include/Gui/Nodes/GViewer.h"
"include/MwaveException.h"
"include/Nodes/Node.h"
"include/Nodes/Read.h"
"include/Nodes/Viewer.h"
"include/mwave.h"
"include/main.h"
"include/shaders.h"
)
set(QOBJECT_HEADERS
"include/Gui/QCompDag.h"
"include/Gui/QPythonEditor.h"
"include/Gui/ViewerWidget.h"
)
set(SOURCES
"src/Application.cpp"
"src/CompDag.cpp"
"src/main.cpp"
"src/mwave.cpp"
"src/Dag.cpp"
"src/Gui/DagView.cpp"
"src/Gui/DagScene.cpp"
"src/Gui/MainWindow.cpp"
"src/Gui/QPythonEditor.cpp"
"src/Gui/GLViewer.cpp"
"src/Gui/ViewerWidget.cpp"
"src/Gui/Nodes/GNode.cpp"
"src/Gui/Nodes/GNodeEdge.cpp"
"src/Gui/QCompDag.cpp"
"src/Nodes/Node.cpp"
"src/Nodes/Read.cpp"
"src/Nodes/Viewer.cpp"
)
QT4_WRAP_CPP(HEADERS_MOC ${QOBJECT_HEADERS})
## Compiler flags
if(CMAKE_COMPILER_IS_GNUCXX)
set(CMAKE_CXX_FLAGS "-O2") ## Optimize
set(CMAKE_CXX_FLAGS "-O3") ## Optimize More
endif()
include_directories(${PYTHON_INCLUDE_DIRS}
${Boost_INCLUDE_DIRS}
${GLEW_INCLUDE_PATH}
${OPENGL_INCLUDE_DIR}
${OIIO_INCLUDE_DIR}
)
add_executable(mwave WIN32 ${HEADERS} ${HEADERS_MOC} ${SOURCES})
set_target_properties(mwave PROPERTIES OUTPUT_NAME mwave.bin)
target_link_libraries( mwave
${PYTHON_LIBRARIES}
${Boost_LIBRARIES}
${OIIO_LIBRARIES}
${GLEW_LIBRARY}
${OPENGL_LIBRARIES}
${QT_LIBRARIES})
cmake 2.6, which you are using, does not support ExternalProjects.
ExternalProjects are supported in cmake 2.8 series. Please move to cmake 2.8.
The below link clarifies
http://www.cmake.org/pipermail/cmake/2011-June/044993.html