[CMake] Append to property COMPILE_DEFINITIONS
Florian Lindner
mailinglists at xgm.de
Tue Jul 18 06:50:28 EDT 2017
Am 18.07.2017 um 18:08 schrieb Craig Scott:
> It might be easier if you showed the whole CMakeLists.txt file so we can see the full picture. Is it available somewhere
> you can link to, or even better if it's small enough, just post it inline here.
Sure! It's not too long, so I'll paste it here
cmake_minimum_required (VERSION 3.1)
project(preCICE)
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR/CMake})
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
endif()
# set_property(GLOBAL APPEND
# PROPERTY COMPILE_DEFINITIONS $<$<CONFIG:Debug>:-DDebug>)
option(PETSC "Enable use of the PETSc linear algebra library." ON)
if (PETSC)
find_library(petsc petsc
PATHS $ENV{PETSC_DIR}/$ENV{PETSC_ARCH}/lib)
if(NOT petsc)
message(FATAL_ERROR "PETSc was not found")
else()
message(STATUS "Using PETSc: $ENV{PETSC_ARCH}")
endif()
endif()
find_package (Threads REQUIRED)
find_package(Boost 1.60.0
REQUIRED
COMPONENTS log log_setup program_options system thread unit_test_framework)
add_definitions(-DBOOST_SPIRIT_USE_PHOENIX_V3 -DBOOST_ALL_DYN_LINK)
option(MPI "Enables MPI-based communication and running coupling tests." ON)
if (MPI)
find_package(MPI REQUIRED)
include_directories(${MPI_INCLUDE_PATH})
set(COMPILE_FLAGS ${COMPILE_FLAGS} ${MPI_COMPILE_FLAGS})
set(LINK_FLAGS ${LINK_FLAGS} ${MPI_LINK_FLAGS})
endif()
find_package(Eigen3 REQUIRED)
include_directories(${EIGEN3_INCLUDE_DIR})
option(PYTHON "Python support" ON)
if (PYTHON)
set(Python_ADDITIONAL_VERSIONS "2.7")
find_package(PythonLibs 2.7 REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})
add_definitions(-DNPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION)
else()
add_definitions(-DPRECICE_NO_PYTHON)
endif()
# Fills the sources* variables
add_subdirectory("src")
# Much target_link_libraries boilerplate
add_library(solib ${sourcesAllNoMain})
set_target_properties(solib
PROPERTIES OUTPUT_NAME libprecice)
target_link_libraries(solib ${PYTHON_LIBRARIES})
target_link_libraries(solib ${MPI_LIBRARIES})
target_link_libraries(solib ${Boost_LIBRARIES})
target_link_libraries(solib ${petsc})
add_library(staticlib ${sourcesAllNoMain})
set_target_properties(staticlib
PROPERTIES OUTPUT_NAME libprecice)
target_link_libraries(staticlib ${PYTHON_LIBRARIES})
target_link_libraries(staticlib ${MPI_LIBRARIES})
target_link_libraries(staticlib ${Boost_LIBRARIES})
target_link_libraries(staticlib ${petsc})
add_executable(precice
"src/drivers/main.cpp" ${sourcesAllNoMain} ${sourcesTarchTests})
target_link_libraries(precice Threads::Threads)
target_link_libraries(precice ${PYTHON_LIBRARIES})
target_link_libraries(precice ${MPI_LIBRARIES})
target_link_libraries(precice ${Boost_LIBRARIES})
target_link_libraries(precice ${petsc})
add_executable(testprecice
"src/testing/main.cpp" ${sourcesAllNoMain} ${sourcesTests})
target_link_libraries(testprecice Threads::Threads)
target_link_libraries(testprecice ${PYTHON_LIBRARIES})
target_link_libraries(testprecice ${MPI_LIBRARIES})
target_link_libraries(testprecice ${Boost_LIBRARIES})
target_link_libraries(testprecice ${petsc})
#Works, but I would prefer to have it just once for all targets and at the top of the file
set_property(TARGET testprecice APPEND
PROPERTY COMPILE_DEFINITIONS "FOO")
> BTW, you don't include the -D when adding to COMPILE_DEFINITIONS, just put FOO, not -DFOO. Have a read of the docs, they may give you other clues for this property (e.g. using the ..._CONFIG variant of it).
Yeah, I found out that I don't have to add -D. The docs say that generator expressions are preferable to using _CONFIG
variant.
Best,
Florian
>
>
> On Tue, Jul 18, 2017 at 1:21 PM, Florian Lindner <mailinglists at xgm.de <mailto:mailinglists at xgm.de>> wrote:
>
> Am 18.07.2017 um 10:59 schrieb Craig Scott:
> > You appear to be setting a GLOBAL property where you probably meant DIRECTORY. You could also consider setting the
> > target property instead rather than applying it to all targets (unless that's what you want).
>
> I tried to set the property on all targets, therefore I thought GLOBAL is the right thing.
>
> However,
>
> set_property(DIRECTORY "${CMAKE_SOURCE_DIR}/src" APPEND
> PROPERTY COMPILE_DEFINITIONS "-DFOO")
>
> hasn't had any effect either.
>
> set_property(TARGET testprecice APPEND
> PROPERTY COMPILE_DEFINITIONS "FOO")
>
> works. But setting it on all targets is exactly what I want. How can I do that?
>
> Best,
> Florian
>
> >
> >
> > On Tue, Jul 18, 2017 at 12:56 PM, Florian Lindner <mailinglists at xgm.de <mailto:mailinglists at xgm.de>
> <mailto:mailinglists at xgm.de <mailto:mailinglists at xgm.de>>> wrote:
> >
> > Hello,
> >
> > I want to add compile definitions. Since I want to use generator expressions, I can't use add_definitions, but
> have to
> > use the COMPILE_DEFINITIONS property, but neither:
> >
> > set_property(GLOBAL APPEND
> > PROPERTY COMPILE_DEFINITIONS "-DFOO")
> >
> > for testing
> >
> > or
> >
> > set_property(GLOBAL APPEND
> > PROPERTY COMPILE_DEFINITIONS $<$<CONFIG:Debug>:-DDebug>)
> >
> > which should be final result, produces any -DFOO compiler switches.
> >
> > I also tried placing it before and after the add_executable call.
> >
> > What is wrong with that call?
> >
> > Thanks,
> > Florian
> > --
> >
> > Powered by www.kitware.com <http://www.kitware.com> <http://www.kitware.com>
> >
> > Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ
> <http://www.cmake.org/Wiki/CMake_FAQ>
> > <http://www.cmake.org/Wiki/CMake_FAQ <http://www.cmake.org/Wiki/CMake_FAQ>>
> >
> > Kitware offers various services to support the CMake community. For more information on each offering, please
> visit:
> >
> > CMake Support: http://cmake.org/cmake/help/support.html <http://cmake.org/cmake/help/support.html>
> <http://cmake.org/cmake/help/support.html <http://cmake.org/cmake/help/support.html>>
> > CMake Consulting: http://cmake.org/cmake/help/consulting.html <http://cmake.org/cmake/help/consulting.html>
> <http://cmake.org/cmake/help/consulting.html <http://cmake.org/cmake/help/consulting.html>>
> > CMake Training Courses: http://cmake.org/cmake/help/training.html <http://cmake.org/cmake/help/training.html>
> <http://cmake.org/cmake/help/training.html <http://cmake.org/cmake/help/training.html>>
> >
> > Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
> <http://www.kitware.com/opensource/opensource.html>
> > <http://www.kitware.com/opensource/opensource.html <http://www.kitware.com/opensource/opensource.html>>
> >
> > Follow this link to subscribe/unsubscribe:
> > http://public.kitware.com/mailman/listinfo/cmake <http://public.kitware.com/mailman/listinfo/cmake>
> <http://public.kitware.com/mailman/listinfo/cmake <http://public.kitware.com/mailman/listinfo/cmake>>
> >
> >
> >
> >
> > --
> > Craig Scott
> > Melbourne, Australia
> > https://crascit.com
>
>
>
>
> --
> Craig Scott
> Melbourne, Australia
> https://crascit.com
More information about the CMake
mailing list