Skip to content

Commit 9aa44fa

Browse files
committed
Use per target and public/private compiler settings.
1 parent d354d06 commit 9aa44fa

File tree

6 files changed

+358
-265
lines changed

6 files changed

+358
-265
lines changed

CMakeLists.txt

Lines changed: 14 additions & 235 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,6 @@
11
### ---[ PCL global CMake
22
cmake_minimum_required(VERSION 3.16.3 FATAL_ERROR)
33

4-
# Set target C++ standard and required compiler features
5-
set(CMAKE_CXX_STANDARD 17 CACHE STRING "The target C++ standard. PCL requires C++14 or higher.")
6-
set(CMAKE_CXX_STANDARD_REQUIRED ON)
7-
set(CMAKE_CXX_EXTENSIONS OFF)
8-
if("${CMAKE_CXX_STANDARD}" GREATER_EQUAL 17)
9-
set(PCL_CXX_COMPILE_FEATURES cxx_std_17)
10-
set(PCL__cplusplus 201703L)
11-
set(PCL_REQUIRES_MSC_VER 1912)
12-
elseif("${CMAKE_CXX_STANDARD}" EQUAL 14)
13-
set(PCL_CXX_COMPILE_FEATURES cxx_std_14)
14-
set(PCL__cplusplus 201402L)
15-
set(PCL_REQUIRES_MSC_VER 1900)
16-
else()
17-
message(FATAL_ERROR "Unknown or unsupported C++ standard specified")
18-
endif()
19-
20-
set(CMAKE_CUDA_STANDARD 17 CACHE STRING "The target CUDA/C++ standard. PCL requires CUDA/C++ 14 or higher.")
21-
set(CMAKE_CUDA_STANDARD_REQUIRED ON)
22-
234
set(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "possible configurations" FORCE)
245

256
# In case the user does not setup CMAKE_BUILD_TYPE, assume it's RelWithDebInfo
@@ -30,10 +11,6 @@ endif()
3011
project(PCL VERSION 1.15.1.99)
3112
string(TOLOWER ${PROJECT_NAME} PROJECT_NAME_LOWER)
3213

33-
if(MSVC AND ("${MSVC_VERSION}" LESS 1910))
34-
message(FATAL_ERROR "The compiler versions prior to Visual Studio version 2017 are not supported. Please upgrade to a newer version or another compiler!")
35-
endif()
36-
3714
### ---[ Find universal dependencies
3815
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules/" ${CMAKE_MODULE_PATH})
3916

@@ -52,44 +29,11 @@ set(CMAKE_BUILD_TYPE "${CMAKE_BUILD_TYPE}" CACHE STRING
5229
"Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel."
5330
FORCE)
5431

55-
# Compiler identification
56-
# Define a variable CMAKE_COMPILER_IS_X where X is the compiler short name.
57-
# Note: CMake automatically defines one for GNUCXX, nothing to do in this case.
58-
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
59-
set(CMAKE_COMPILER_IS_CLANG 1)
60-
elseif(__COMPILER_PATHSCALE)
61-
set(CMAKE_COMPILER_IS_PATHSCALE 1)
62-
elseif(MSVC)
63-
set(CMAKE_COMPILER_IS_MSVC 1)
64-
elseif(MINGW)
65-
set(CMAKE_COMPILER_IS_MINGW 1)
66-
endif()
67-
6832
# Ensure try_compile sets the include paths (e.g. for Eigen3)
6933
set(CMAKE_TRY_COMPILE_CONFIGURATION ${CMAKE_BUILD_TYPE})
7034

71-
# https://github.com/fish-shell/fish-shell/issues/5865
72-
include(CheckCXXSourceCompiles)
73-
CHECK_CXX_SOURCE_COMPILES("
74-
#include <atomic>
75-
struct big { int foo[64]; };
76-
std::atomic<big> x;
77-
int main() {
78-
return x.load().foo[13];
79-
}"
80-
LIBATOMIC_NOT_NEEDED)
81-
IF (NOT LIBATOMIC_NOT_NEEDED)
82-
SET(ATOMIC_LIBRARY "atomic")
83-
ENDIF()
84-
85-
# Create a variable with expected default CXX flags
86-
# This will be used further down the road to check if the user explicitly provided CXX flags
87-
if(CMAKE_COMPILER_IS_MSVC)
88-
set(CMAKE_CXX_FLAGS_DEFAULT ${CMAKE_CXX_FLAGS_INIT})
89-
string(STRIP ${CMAKE_CXX_FLAGS_DEFAULT} CMAKE_CXX_FLAGS_DEFAULT)
90-
else()
91-
set(CMAKE_CXX_FLAGS_DEFAULT "")
92-
endif()
35+
# Check if we need to link against libatomic (armel)
36+
include("${PCL_SOURCE_DIR}/cmake/pcl_check_atomic.cmake")
9337

9438
include("${PCL_SOURCE_DIR}/cmake/pcl_verbosity.cmake")
9539
include("${PCL_SOURCE_DIR}/cmake/pcl_targets.cmake")
@@ -100,158 +44,28 @@ DISSECT_VERSION()
10044
GET_OS_INFO()
10145
SET_INSTALL_DIRS()
10246

103-
104-
if(${PCL_ENABLE_CCACHE})
105-
include (UseCompilerCache)
106-
UseCompilerCache(ccache REQUIRED)
107-
endif()
47+
# Check/Set compiler settings
48+
include("cmake/pcl_set_compiler_settings.cmake")
49+
message(STATUS "PCL public Options: ${PCL_PUBLIC_COMPILER_OPTIONS}")
50+
message(STATUS "PCL private Options: ${PCL_PRIVATE_COMPILER_OPTIONS}")
51+
message(STATUS "PCL public Definitions: ${PCL_PUBLIC_COMPILER_DEFINITIONS}")
52+
message(STATUS "PCL private Definitions: ${PCL_PRIVATE_COMPILER_DEFINITIONS}")
53+
message(STATUS "PCL public Compile Features: ${PCL_PUBLIC_COMPILER_FEATURES}")
54+
message(STATUS "PCL private Compile Features: ${PCL_PRIVATE_COMPILER_FEATURES}")
55+
message(STATUS "PCL private Link Options: ${PCL_PRIVATE_LINK_OPTIONS}")
10856

10957
# Enable verbose timing display?
11058
if(CMAKE_TIMING_VERBOSE AND UNIX)
11159
set_property(GLOBAL PROPERTY RULE_MESSAGES OFF)
11260
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CMAKE_SOURCE_DIR}/cmake/custom_output.sh")
11361
endif()
11462

115-
# check for allocation functions that return aligned memory
116-
include("${PCL_SOURCE_DIR}/cmake/pcl_alignment.cmake")
117-
PCL_CHECK_FOR_ALIGNMENT()
118-
119-
# check for SSE flags
120-
include("${PCL_SOURCE_DIR}/cmake/pcl_find_sse.cmake")
121-
if(PCL_ENABLE_SSE AND "${CMAKE_CXX_FLAGS}" STREQUAL "${CMAKE_CXX_FLAGS_DEFAULT}")
122-
PCL_CHECK_FOR_SSE()
123-
endif()
124-
125-
# check for AVX flags
126-
if(PCL_ENABLE_AVX AND "${CMAKE_CXX_FLAGS}" STREQUAL "${CMAKE_CXX_FLAGS_DEFAULT}")
127-
include("${PCL_SOURCE_DIR}/cmake/pcl_find_avx.cmake")
128-
PCL_CHECK_FOR_AVX()
129-
endif()
130-
13163
# Cuda
13264
option(WITH_CUDA "Build NVIDIA-CUDA support" TRUE)
13365
if(WITH_CUDA)
13466
include("${PCL_SOURCE_DIR}/cmake/pcl_find_cuda.cmake")
13567
endif()
13668

137-
138-
# ---[ Unix/Darwin/Windows specific flags
139-
if(CMAKE_COMPILER_IS_GNUCXX)
140-
if("${CMAKE_CXX_FLAGS}" STREQUAL "${CMAKE_CXX_FLAGS_DEFAULT}")
141-
if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7)
142-
string(APPEND CMAKE_CXX_FLAGS " -Wabi=18")
143-
else()
144-
string(APPEND CMAKE_CXX_FLAGS " -Wabi")
145-
endif()
146-
string(APPEND CMAKE_CXX_FLAGS " -Wall -Wextra -fno-strict-aliasing ${SSE_FLAGS} ${AVX_FLAGS}")
147-
endif()
148-
149-
if(PCL_WARNINGS_ARE_ERRORS)
150-
string(APPEND CMAKE_CXX_FLAGS " -Werror -fno-strict-aliasing")
151-
endif()
152-
153-
if("${CMAKE_SHARED_LINKER_FLAGS}" STREQUAL "" AND NOT CMAKE_SYSTEM_NAME STREQUAL "Darwin")
154-
set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--as-needed")
155-
endif()
156-
157-
if(WIN32)
158-
if(PCL_SHARED_LIBS)
159-
string(APPEND CMAKE_SHARED_LINKER_FLAGS " -Wl,--export-all-symbols -Wl,--enable-auto-import")
160-
if(MINGW)
161-
add_definitions("-DBOOST_THREAD_USE_LIB")
162-
string(APPEND CMAKE_SHARED_LINKER_FLAGS " -Wl,--allow-multiple-definition")
163-
endif()
164-
else()
165-
add_definitions("-DBOOST_LIB_DIAGNOSTIC -DBOOST_THREAD_USE_LIB")
166-
endif()
167-
endif()
168-
endif()
169-
170-
if(CMAKE_COMPILER_IS_MSVC)
171-
add_definitions("-DBOOST_ALL_NO_LIB -D_SCL_SECURE_NO_WARNINGS -D_CRT_SECURE_NO_WARNINGS -DNOMINMAX ${SSE_DEFINITIONS}")
172-
173-
if("${CMAKE_CXX_FLAGS}" STREQUAL "${CMAKE_CXX_FLAGS_DEFAULT}")
174-
175-
string(APPEND CMAKE_CXX_FLAGS " /fp:precise ${SSE_FLAGS} ${AVX_FLAGS}")
176-
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -Xcompiler=/bigobj")
177-
178-
set(PCL_USE_GLOBAL_OPTIMIZATION TRUE)
179-
if(CUDA_FOUND)
180-
if(${CUDA_VERSION_STRING} VERSION_GREATER_EQUAL "10.0" AND ${CUDA_VERSION_STRING} VERSION_LESS "12.0")
181-
set(PCL_USE_GLOBAL_OPTIMIZATION FALSE)
182-
message("Global optimizations /GL has been turned off, as it doesn't work with nvcc/thrust in CUDA 10 and 11.")
183-
endif()
184-
endif()
185-
186-
# Add extra code generation/link optimizations
187-
if(CMAKE_MSVC_CODE_LINK_OPTIMIZATION AND PCL_USE_GLOBAL_OPTIMIZATION)
188-
string(APPEND CMAKE_CXX_FLAGS_RELEASE " /GL")
189-
string(APPEND CMAKE_SHARED_LINKER_FLAGS_RELEASE " /LTCG /OPT:REF")
190-
string(APPEND CMAKE_EXE_LINKER_FLAGS_RELEASE " /LTCG")
191-
endif()
192-
# /MANIFEST:NO") # please, don't disable manifest generation, otherwise crash at start for vs2008
193-
194-
# Disable some warnings
195-
string(APPEND CMAKE_CXX_FLAGS " /wd4800 /wd4521 /wd4251 /wd4275 /wd4305 /wd4355")
196-
197-
# Enable warnings, which are disabled by default (see https://learn.microsoft.com/de-de/cpp/preprocessor/compiler-warnings-that-are-off-by-default)
198-
string(APPEND CMAKE_CXX_FLAGS " /w34265")
199-
200-
if(PCL_WARNINGS_ARE_ERRORS)
201-
# MSVC supports external includes only since Visual Studio 2019 version 16.10.0.
202-
# CMake supports external includes since 3.22.0 using the Ninja generator or NMake files (see https://gitlab.kitware.com/cmake/cmake/-/merge_requests/4766)
203-
# CMake supports external includes for Visual Studio also since 3.24.0 (see https://gitlab.kitware.com/cmake/cmake/-/merge_requests/7238)
204-
if(CMAKE_C_COMPILER_VERSION VERSION_LESS "19.29.30036.3" OR CMAKE_VERSION VERSION_LESS 3.22.0 OR (CMAKE_VERSION VERSION_LESS 3.24.0 AND CMAKE_GENERATOR MATCHES "Visual Studio"))
205-
message(WARNING "With the used combination of compiler and CMake version it is not recommended to activate PCL_WARNINGS_ARE_ERRORS, "
206-
"because also warnings from 3rd party components are marked as errors. It is recommended to upgrade to "
207-
"Visual Studio 2019 version 16.10.0 and CMake 3.24.0 (or CMake 3.22.0 if using Ninja or NMake files).")
208-
endif()
209-
string(APPEND CMAKE_CXX_FLAGS " /WX")
210-
endif()
211-
212-
include(ProcessorCount)
213-
ProcessorCount(CPUCores)
214-
set(MSVC_MP ${CPUCores} CACHE STRING "Number of simultaneously running compilers (0 = automatic detection by MSVC). See documentation of /MP flag.")
215-
216-
if(MSVC_MP EQUAL 0)
217-
# MSVC_MP is 0 in case the information cannot be determined by ProcessorCount => fallback
218-
# Generator expression is necessary to limit /MP flag to C/CXX, so flag will be not set to e.g. CUDA (see https://gitlab.kitware.com/cmake/cmake/issues/17535)
219-
add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:/MP>)
220-
elseif(MSVC_MP GREATER 1)
221-
add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:/MP${MSVC_MP}>)
222-
endif()
223-
endif()
224-
string(APPEND CMAKE_CXX_FLAGS " /bigobj")
225-
226-
if(CMAKE_GENERATOR STREQUAL "Ninja")
227-
string(APPEND CMAKE_C_FLAGS " /FS")
228-
string(APPEND CMAKE_CXX_FLAGS " /FS")
229-
endif()
230-
endif()
231-
232-
if(CMAKE_COMPILER_IS_PATHSCALE)
233-
if("${CMAKE_CXX_FLAGS}" STREQUAL "${CMAKE_CXX_FLAGS_DEFAULT}")
234-
set(CMAKE_CXX_FLAGS "-Wno-uninitialized -zerouv -mp")
235-
endif()
236-
if("${CMAKE_SHARED_LINKER_FLAGS}" STREQUAL "")
237-
set(CMAKE_SHARED_LINKER_FLAGS "-mp")
238-
endif()
239-
endif()
240-
241-
if(CMAKE_COMPILER_IS_CLANG)
242-
if("${CMAKE_C_FLAGS}" STREQUAL "${CMAKE_CXX_FLAGS_DEFAULT}")
243-
string(APPEND CMAKE_C_FLAGS " -Wall -Wextra")
244-
endif()
245-
if("${CMAKE_CXX_FLAGS}" STREQUAL "")
246-
set(CMAKE_CXX_FLAGS "-ftemplate-depth=1024 -Wno-invalid-offsetof ${SSE_FLAGS} ${AVX_FLAGS}") # Unfortunately older Clang versions do not have this: -Wno-unnamed-type-template-args
247-
if(APPLE AND WITH_CUDA AND CUDA_FOUND)
248-
string(APPEND CMAKE_CXX_FLAGS " -stdlib=libstdc++")
249-
endif()
250-
string(APPEND CMAKE_CXX_FLAGS " -Wall -Wextra")
251-
endif()
252-
set(CLANG_LIBRARIES "stdc++")
253-
endif()
254-
25569
if(WIN32)
25670
set(PCL_RESOURCES_DIR "${PCL_SOURCE_DIR}/resources")
25771
set(PCL_POINTCLOUDS_DIR "${PCL_RESOURCES_DIR}/pointclouds")
@@ -306,44 +120,6 @@ set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${LIB_INSTALL_DIR}")
306120
endif()
307121

308122
### ---[ Find universal dependencies
309-
310-
# OpenMP (optional)
311-
option(WITH_OPENMP "Build with parallelization using OpenMP" TRUE)
312-
option(USE_HOMEBREW_FALLBACK "(macOS-only) also look in 'brew --prefix' for libraries (e.g. OpenMP)" TRUE)
313-
if(WITH_OPENMP)
314-
find_package(OpenMP COMPONENTS C CXX)
315-
if(APPLE AND NOT OpenMP_FOUND)
316-
if(USE_HOMEBREW_FALLBACK)
317-
# libomp 15.0+ from brew is keg-only, so have to search in other locations.
318-
# See https://github.com/Homebrew/homebrew-core/issues/112107#issuecomment-1278042927.
319-
execute_process(COMMAND brew --prefix libomp
320-
OUTPUT_VARIABLE HOMEBREW_LIBOMP_PREFIX
321-
OUTPUT_STRIP_TRAILING_WHITESPACE)
322-
set(OpenMP_C_FLAGS "-Xpreprocessor -fopenmp -I${HOMEBREW_LIBOMP_PREFIX}/include")
323-
set(OpenMP_CXX_FLAGS "-Xpreprocessor -fopenmp -I${HOMEBREW_LIBOMP_PREFIX}/include")
324-
set(OpenMP_C_LIB_NAMES omp)
325-
set(OpenMP_CXX_LIB_NAMES omp)
326-
set(OpenMP_omp_LIBRARY ${HOMEBREW_LIBOMP_PREFIX}/lib/libomp.dylib)
327-
find_package(OpenMP COMPONENTS C CXX)
328-
endif()
329-
endif()
330-
endif()
331-
if(OpenMP_FOUND)
332-
string(APPEND CMAKE_C_FLAGS " ${OpenMP_C_FLAGS}")
333-
string(APPEND CMAKE_CXX_FLAGS " ${OpenMP_CXX_FLAGS}")
334-
335-
# We could use OpenMP_CXX_VERSION starting from CMake 3.9, but this value is only available on first run of CMake (see https://gitlab.kitware.com/cmake/cmake/issues/19150),
336-
# so we use always OpenMP_CXX_SPEC_DATE, which is available since CMake 3.7.
337-
message(STATUS "Found OpenMP, spec date ${OpenMP_CXX_SPEC_DATE}")
338-
339-
if((MSVC_VERSION EQUAL 1900) OR (MSVC_VERSION MATCHES "^191[0-9]$"))
340-
string(APPEND CMAKE_SHARED_LINKER_FLAGS_DEBUG " /DELAYLOAD:VCOMP140D.dll")
341-
string(APPEND CMAKE_SHARED_LINKER_FLAGS_RELEASE " /DELAYLOAD:VCOMP140.dll")
342-
endif()
343-
else()
344-
message(STATUS "Not found OpenMP")
345-
endif()
346-
347123
# Threads (required)
348124
find_package(Threads REQUIRED)
349125

@@ -471,6 +247,8 @@ if(WITH_SYSTEM_CJSON)
471247
endif()
472248

473249
set(CMAKE_REQUIRED_LIBRARIES Eigen3::Eigen) # so that Eigen/Core is found below
250+
string(REPLACE ";" " " PCL_PUBLIC_COMPILE_OPTIONS_STR "${PCL_PUBLIC_COMPILER_OPTIONS}")
251+
set(CMAKE_REQUIRED_FLAGS ${PCL_PUBLIC_COMPILE_OPTIONS_STR})
474252
CHECK_CXX_SOURCE_COMPILES("
475253
#include <Eigen/Core>
476254
#if (EIGEN_DEFAULT_ALIGN_BYTES==0) || EIGEN_MALLOC_ALREADY_ALIGNED
@@ -484,6 +262,7 @@ else()
484262
set(PCL_USES_EIGEN_HANDMADE_ALIGNED_MALLOC 0)
485263
endif()
486264
unset(CMAKE_REQUIRED_LIBRARIES)
265+
unset(CMAKE_REQUIRED_FLAGS)
487266

488267
### --[ Set variables for pcl_config.h
489268
if (BUILD_visualization)

cmake/pcl_check_atomic.cmake

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# https://github.com/fish-shell/fish-shell/issues/5865
2+
include(CheckCXXSourceCompiles)
3+
CHECK_CXX_SOURCE_COMPILES("
4+
#include <atomic>
5+
struct big { int foo[64]; };
6+
std::atomic<big> x;
7+
int main() {
8+
return x.load().foo[13];
9+
}"
10+
LIBATOMIC_NOT_NEEDED)
11+
IF (NOT LIBATOMIC_NOT_NEEDED)
12+
SET(ATOMIC_LIBRARY "atomic")
13+
ENDIF()

cmake/pcl_find_avx.cmake

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ function(PCL_CHECK_FOR_AVX)
3939
# and this allows the compiler to use the codes for AVX behind code guards.
4040
if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX OR CMAKE_COMPILER_IS_CLANG)
4141
if(HAVE_AVX2)
42-
set(AVX_FLAGS "-mavx2" PARENT_SCOPE)
42+
list(APPEND AVX_FLAGS "-mavx2")
4343
elseif(HAVE_AVX)
44-
set(AVX_FLAGS "-mavx" PARENT_SCOPE)
44+
list(APPEND AVX_FLAGS "-mavx")
4545
endif()
4646
endif()
4747

@@ -51,9 +51,11 @@ function(PCL_CHECK_FOR_AVX)
5151
# TODO: Add AVX512 variant if needed.
5252
if(MSVC)
5353
if(HAVE_AVX2)
54-
set(AVX_FLAGS "/arch:AVX2" PARENT_SCOPE)
54+
list(APPEND AVX_FLAGS "/arch:AVX2")
5555
elseif(HAVE_AVX)
56-
set(AVX_FLAGS "/arch:AVX" PARENT_SCOPE)
56+
list(APPEND AVX_FLAGS "/arch:AVX")
5757
endif()
5858
endif()
59+
60+
set(AVX_FLAGS ${AVX_FLAGS} PARENT_SCOPE)
5961
endfunction()

0 commit comments

Comments
 (0)