Skip to content

Commit 67d9862

Browse files
committed
take review comments into account
* protect GCC-specific stuff * use `list(APPEND...)` * use CMake-style way to choose whether LTO should/can be done or not * only install public header, not all Signed-off-by: Steffen Jaeckel <[email protected]>
1 parent 1ed0751 commit 67d9862

File tree

1 file changed

+37
-25
lines changed

1 file changed

+37
-25
lines changed

CMakeLists.txt

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ set(PACKAGE_RELEASE_VERSION 1)
2121
# Include cmake modules
2222
#-----------------------------------------------------------------------------
2323
include(GNUInstallDirs)
24+
include(CheckIPOSupported)
2425
include(CMakePackageConfigHelpers)
26+
# default is "No tests"
27+
option(BUILD_TESTING "" OFF)
28+
include(CTest)
2529
include(sources.cmake)
2630

2731
# The only direct cmake argument for now
@@ -31,42 +35,41 @@ option(BUILD_SHARED_LIBS "Build shared library and only the shared library if \"
3135
# Compose CFLAGS
3236
#-----------------------------------------------------------------------------
3337

34-
# Some information copied from makefile_include.mk
38+
# Some information ported from makefile_include.mk
3539

36-
# Basic set
37-
set(LTM_C_FLAGS -Wall -Wsign-compare -Wextra -Wshadow)
38-
set(LTM_C_FLAGS ${LTM_C_FLAGS} -Wdeclaration-after-statement -Wbad-function-cast -Wcast-align)
39-
set(LTM_C_FLAGS ${LTM_C_FLAGS} -Wstrict-prototypes -Wpointer-arith -Wsystem-headers)
4040

4141
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
4242
message(STATUS "Setting build type to 'Release' as none was specified.")
4343
set(CMAKE_BUILD_TYPE "Release")
4444
endif()
4545

46-
set(CMAKE_C_FLAGS_DEBUG "-g3")
47-
set(CMAKE_C_FLAGS_RELEASE "-O3 -funroll-loops -fomit-frame-pointer")
48-
set(CMAKE_C_FLAGS_RELWITHDEBINFO "-g3 -O2")
49-
set(CMAKE_C_FLAGS_MINSIZEREL "-Os")
50-
51-
if(COMPILE_LTO)
52-
set(LTM_C_FLAGS ${LTM_C_FLAGS} -flto)
53-
set(LTM_LD_FLAGS ${LTM_LD_FLAGS} -flto)
46+
# We only differentiate between MSVC and GCC-compatible compilers
47+
if(MSVC)
48+
set(LTM_C_FLAGS -W3)
49+
else()
50+
set(LTM_C_FLAGS -Wall -Wsign-compare -Wextra -Wshadow
51+
-Wdeclaration-after-statement -Wbad-function-cast -Wcast-align
52+
-Wstrict-prototypes -Wpointer-arith -Wsystem-headers)
53+
set(CMAKE_C_FLAGS_DEBUG "-g3")
54+
set(CMAKE_C_FLAGS_RELEASE "-O3 -funroll-loops -fomit-frame-pointer")
55+
set(CMAKE_C_FLAGS_RELWITHDEBINFO "-g3 -O2")
56+
set(CMAKE_C_FLAGS_MINSIZEREL "-Os")
5457
endif()
5558

5659
# What compiler do we have and what are their...uhm... peculiarities
5760
if(CMAKE_C_COMPILER_ID MATCHES "(C|c?)lang")
58-
set(LTM_C_FLAGS ${LTM_C_FLAGS} -Wno-typedef-redefinition -Wno-tautological-compare -Wno-builtin-requires-header)
61+
list(APPEND LTM_C_FLAGS -Wno-typedef-redefinition -Wno-tautological-compare -Wno-builtin-requires-header)
5962
# Clang requires at least '-O1' for dead code eliminiation
60-
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -O1")
63+
set(CMAKE_C_FLAGS_DEBUG "-O1 ${CMAKE_C_FLAGS_DEBUG}")
6164
endif()
6265
if(CMAKE_C_COMPILER MATCHES "mingw")
63-
set(LTM_C_FLAGS ${LTM_C_FLAGS} -Wno-shadow)
66+
list(APPEND LTM_C_FLAGS -Wno-shadow -Wno-expansion-to-defined -Wno-declaration-after-statement -Wno-bad-function-cast)
6467
endif()
6568
if(CMAKE_SYSTEM_NAME MATCHES "Darwin")
66-
set(LTM_C_FLAGS ${LTM_C_FLAGS} -Wno-nullability-completeness)
69+
list(APPEND LTM_C_FLAGS -Wno-nullability-completeness)
6770
endif()
6871
if(CMAKE_SYSTEM_NAME MATCHES "CYGWIN")
69-
set(LTM_C_FLAGS ${LTM_C_FLAGS} -no-undefined)
72+
list(APPEND LTM_C_FLAGS -no-undefined)
7073
endif()
7174

7275
# TODO: coverage (lgcov)
@@ -75,8 +78,8 @@ endif()
7578
# in order to allow overriding our defaults.
7679
# ${LTM_CFLAGS} means the user passed it via sth like:
7780
# $ cmake -DLTM_CFLAGS="foo"
78-
set(LTM_C_FLAGS ${LTM_C_FLAGS} ${LTM_CFLAGS})
79-
set(LTM_LD_FLAGS ${LTM_LD_FLAGS} ${LTM_LDFLAGS})
81+
list(APPEND LTM_C_FLAGS ${LTM_CFLAGS})
82+
list(APPEND LTM_LD_FLAGS ${LTM_LDFLAGS})
8083

8184
#-----------------------------------------------------------------------------
8285
# library target
@@ -100,13 +103,25 @@ target_link_options(${PROJECT_NAME} BEFORE PRIVATE
100103
set_target_properties(${PROJECT_NAME} PROPERTIES
101104
VERSION ${PROJECT_VERSION}
102105
SOVERSION ${PROJECT_VERSION_MAJOR}
106+
PUBLIC_HEADER tommath.h
103107
)
104108

109+
option(COMPILE_LTO "Build with LTO enabled")
110+
check_ipo_supported(RESULT result)
111+
if(result AND COMPILE_LTO)
112+
set_property(TARGET ${PROJECT_NAME} PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
113+
elseif(COMPILE_LTO)
114+
message(WARNING "LTO is not supported for this compiler.")
115+
endif()
105116

106117
#-----------------------------------------------------------------------------
107118
# demo target
108119
#-----------------------------------------------------------------------------
109-
add_subdirectory(demo)
120+
121+
if(BUILD_TESTING)
122+
enable_testing()
123+
add_subdirectory(demo)
124+
endif()
110125

111126
#-----------------------------------------------------------------------------
112127
# Install/export targets and files
@@ -120,10 +135,7 @@ install(TARGETS ${PROJECT_NAME}
120135
EXPORT ${TARGETS_EXPORT_NAME}
121136
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
122137
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
123-
)
124-
125-
install(FILES ${HEADERS}
126-
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}
138+
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}
127139
)
128140

129141
# Install libtommath.pc for pkg-config if we build a shared library

0 commit comments

Comments
 (0)