Skip to content

Commit

Permalink
CMakeLists.txt: export target gemmi_headers
Browse files Browse the repository at this point in the history
instead of setting CMAKE_CXX_STANDARD set compile features to cxx_std_11
  • Loading branch information
wojdyr committed Sep 14, 2023
1 parent 3f5d879 commit b6fb603
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 42 deletions.
12 changes: 6 additions & 6 deletions .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
- arch: x64
CMAKE_GENERATOR: Visual Studio 16 2019
CMAKE_CONFIG: RelWithDebInfo
CMAKE_ARGS: -DCMAKE_CXX_STANDARD=11 -DUSE_PYTHON=1 -DPython_EXECUTABLE="C:\\Python38-x64\\python.exe"
CMAKE_ARGS: -DCMAKE_CXX_STANDARD=11 -DUSE_PYTHON=1 -DPython_EXECUTABLE="C:\\Python310-x64\\python.exe"
APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2019
PY_PYTHON: 3.8
PY_PYTHON: 3.10
USE_PIP: 1
- arch: x86
CMAKE_GENERATOR: Visual Studio 15 2017
CMAKE_CONFIG: MinSizeRel
CMAKE_ARGS: -DUSE_PYTHON=1 -DPython_EXECUTABLE="C:\\Python310\\python.exe"
CMAKE_ARGS: -DUSE_PYTHON=1 -DPython_EXECUTABLE="C:\\Python38\\python.exe"
APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017
PY_PYTHON: 3.10-32
PY_PYTHON: 3.8-32
- arch: x64
CMAKE_GENERATOR: MSYS Makefiles
CMAKE_ARGS: -DCMAKE_CXX_STANDARD=14
Expand All @@ -30,7 +30,7 @@
CMAKE_CONFIG: Release
APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2019
PY_PYTHON: 3.9-32
USE_SETUP_PY: 1
USE_PIP_INSTALL: 1

build_script:
- if defined BINDIR (set "PATH=%BINDIR%;%PATH%")
Expand All @@ -47,7 +47,7 @@
- if exist "%CMAKE_CONFIG%\gemmi*.pyd" (copy "%CMAKE_CONFIG%\gemmi*.pyd" . /y)
- dir C:\mingw-w64\
- py -c "import sys; print(sys.version+'\n'+sys.executable)"
- if defined USE_SETUP_PY (py setup.py install)
- if defined USE_PIP_INSTALL (py -m pip install .)
- cmd: '"%VS140COMNTOOLS%\..\..\VC\vcvarsall.bat" %ARCH%'

test_script:
Expand Down
70 changes: 34 additions & 36 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,7 @@ if (DEFINED ENV{EXTRA_WFLAGS})
set(EXTRA_WARNINGS ON CACHE BOOL "Set extra warning flags" FORCE)
endif()

if (NOT CMAKE_CXX_STANDARD)
if (CMAKE_CXX20_STANDARD_COMPILE_OPTION)
set(CMAKE_CXX_STANDARD 20)
elseif (CMAKE_CXX17_STANDARD_COMPILE_OPTION)
# Python bindings don't compile as C++17 in VS 2017
if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
set(CMAKE_CXX_STANDARD 14)
else()
set(CMAKE_CXX_STANDARD 17)
endif()
elseif (CMAKE_CXX11_STANDARD_COMPILE_OPTION)
set(CMAKE_CXX_STANDARD 11)
endif()
endif()
message(STATUS "Compiling with C++ standard: ${CMAKE_CXX_STANDARD}")
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

include(CheckIncludeFile) # for check_include_file
Expand Down Expand Up @@ -123,9 +106,6 @@ else (NOT benchmark_FOUND)
message(STATUS "Benchmarks not configured.")
endif()

include_directories("${CMAKE_CURRENT_SOURCE_DIR}/include"
"${CMAKE_CURRENT_SOURCE_DIR}/third_party")

if (CMAKE_CXX_COMPILER_ID MATCHES "Clang|GNU" AND EXTRA_WARNINGS)
set(CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic -Wformat=2 -Wredundant-decls -Wfloat-conversion -Wdisabled-optimization -Wshadow $ENV{EXTRA_WFLAGS}")
Expand Down Expand Up @@ -189,13 +169,13 @@ endif()
# target_link_libraries(fgemmi PRIVATE cgemmi)
#endif()


### programs from prog/ ###

add_library(options OBJECT prog/options.cpp)
if (GEMMI_VERSION_INFO)
target_compile_definitions(options PRIVATE GEMMI_VERSION_INFO=${GEMMI_VERSION_INFO})
endif()
add_library(gemmi_headers INTERFACE)
add_library(gemmi::gemmi_headers ALIAS gemmi_headers)
add_library(gemmi::headers ALIAS gemmi_headers)
target_include_directories(gemmi_headers INTERFACE
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")
target_compile_features(gemmi_headers INTERFACE cxx_std_11)

add_library(gemmi_cpp
src/assembly.cpp src/calculate.cpp src/crd.cpp
Expand All @@ -207,26 +187,36 @@ add_library(gemmi_cpp
set_property(TARGET gemmi_cpp PROPERTY POSITION_INDEPENDENT_CODE ON)
#set_property(TARGET gemmi_cpp PROPERTY CXX_VISIBILITY_PRESET hidden)
target_compile_definitions(gemmi_cpp PRIVATE GEMMI_BUILD)
target_include_directories(gemmi_cpp PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/third_party")

if (BUILD_SHARED_LIBS)
target_compile_definitions(gemmi_cpp PUBLIC GEMMI_SHARED)
endif()
if (DEFINED CMAKE_CXX_STANDARD)
target_compile_features(gemmi_cpp PUBLIC "cxx_std_${CMAKE_CXX_STANDARD}")
endif()
target_link_libraries(gemmi_cpp PUBLIC gemmi_headers)
support_gz(gemmi_cpp)
target_include_directories(gemmi_cpp PUBLIC
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")

### programs from prog/ ###

add_library(options OBJECT prog/options.cpp)
target_link_libraries(options PUBLIC gemmi_headers)
target_include_directories(options PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/third_party")
if (GEMMI_VERSION_INFO)
target_compile_definitions(options PRIVATE GEMMI_VERSION_INFO=${GEMMI_VERSION_INFO})
endif()

# Gemmi subcommands compiled as individual binaries.
# Not built by default and used mostly for testing.
function(add_single_prog target)
cmake_parse_arguments(arg "NOLIB;GZ" "ADD_SRC" "" ${ARGN})
add_executable(gemmi-${target} EXCLUDE_FROM_ALL prog/${target}.cpp ${arg_ADD_SRC}
$<TARGET_OBJECTS:options>)
if (NOT arg_NOLIB)
if (arg_NOLIB)
target_link_libraries(gemmi-${target} PRIVATE gemmi_headers)
else()
target_link_libraries(gemmi-${target} PRIVATE gemmi_cpp)
endif()
target_include_directories(gemmi-${target} PRIVATE
"${CMAKE_CURRENT_SOURCE_DIR}/third_party")
if (arg_GZ)
support_gz(gemmi-${target})
endif()
Expand Down Expand Up @@ -281,6 +271,7 @@ add_executable(gemmi_prog
$<TARGET_OBJECTS:options>)
target_link_libraries(gemmi_prog PRIVATE gemmi_cpp)
support_gz(gemmi_prog)
target_include_directories(gemmi_prog PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/third_party")
target_compile_definitions(gemmi_prog PRIVATE GEMMI_ALL_IN_ONE=1)
set_property(TARGET gemmi_prog PROPERTY OUTPUT_NAME gemmi)
if (WIN32 AND USE_WMAIN)
Expand Down Expand Up @@ -312,12 +303,17 @@ endif()
add_executable(cpptest EXCLUDE_FROM_ALL tests/main.cpp tests/cif.cpp
src/mtz2cif.cpp src/eig3.cpp src/sprintf.cpp)
target_compile_definitions(cpptest PRIVATE USE_STD_SNPRINTF=1)
target_link_libraries(cpptest PRIVATE gemmi_headers)
target_include_directories(cpptest PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/third_party")

add_executable(hello EXCLUDE_FROM_ALL examples/hello.cpp)
target_link_libraries(hello PRIVATE gemmi_headers)
add_executable(doc_example EXCLUDE_FROM_ALL
docs/code/sym.cpp docs/code/elem.cpp docs/code/resinfo.cpp
docs/code/cell.cpp src/resinfo.cpp)
target_link_libraries(doc_example PRIVATE gemmi_headers)
add_executable(doc_example2 EXCLUDE_FROM_ALL docs/code/cif_cc.cpp)
target_link_libraries(doc_example2 PRIVATE gemmi_headers)
add_executable(doc_maybegz EXCLUDE_FROM_ALL docs/code/maybegz.cpp docs/code/mutate.cpp)
target_link_libraries(doc_maybegz PRIVATE gemmi_cpp)
support_gz(doc_maybegz)
Expand All @@ -336,6 +332,7 @@ support_gz(test_disulf)

# auth_label requires <experimental/filesystem> and -lstdc++fs
add_executable(auth_label EXCLUDE_FROM_ALL examples/auth_label.cpp)
target_link_libraries(auth_label PRIVATE gemmi_headers)
if (NOT MSVC)
target_link_libraries(auth_label PRIVATE stdc++fs)
endif()
Expand Down Expand Up @@ -381,7 +378,7 @@ if (benchmark_FOUND)
if (b MATCHES "resinfo|pdb|writecif")
target_link_libraries(${b}-bm PRIVATE gemmi_cpp)
endif()
target_link_libraries(${b}-bm PRIVATE benchmark::benchmark)
target_link_libraries(${b}-bm PRIVATE gemmi_headers benchmark::benchmark)
set_property(TARGET ${b}-bm PROPERTY RUNTIME_OUTPUT_DIRECTORY
"${CMAKE_BINARY_DIR}/benchmarks")
add_dependencies(check ${b}-bm)
Expand Down Expand Up @@ -416,6 +413,7 @@ if (USE_PYTHON)
python/sf.cpp python/sym.cpp python/topo.cpp
python/unitcell.cpp python/write.cpp
$<TARGET_OBJECTS:gemmi_cpp>)
target_link_libraries(gemmi_py PRIVATE gemmi_headers)
set_property(TARGET gemmi_py PROPERTY OUTPUT_NAME gemmi)
if(CMAKE_CXX_FLAGS MATCHES "-Wshadow")
target_compile_options(gemmi_py PRIVATE "-Wno-shadow")
Expand Down Expand Up @@ -444,7 +442,7 @@ if (BUILD_GEMMI_PROGRAM)
# Is it useful to have the program location in gemmi-targets.cmake?
install(TARGETS gemmi_prog EXPORT GemmiTargets)
endif()
install(TARGETS gemmi_cpp
install(TARGETS gemmi_headers gemmi_cpp
EXPORT GemmiTargets
INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")

Expand Down

0 comments on commit b6fb603

Please sign in to comment.