Skip to content

Commit d5134a7

Browse files
committed
CMake support
- initial cmake support - downgrade cmake requirement to 2.8.12 - factorize cmake test flags / libs options - refactor test / library under helpers functions, follow bazel's style - Add fix for MSVC and Windows support ( thx @patrikfors ) - Switch to default "add_subdirectory()" usage mode - add CMake/README.md for instructions - add header-only cmake target generator - map absl target to absl:: namespace
1 parent 962e993 commit d5134a7

17 files changed

+1766
-0
lines changed

CMake/AbseilHelpers.cmake

+157
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
#
2+
# Copyright 2017 The Abseil Authors.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
include(CMakeParseArguments)
18+
19+
set(_ABSL_HELPERS_PATH "${CMAKE_CURRENT_LIST_DIR}")
20+
21+
#
22+
# create a static library absl_based on the following variable
23+
#
24+
# parameters
25+
# SOURCES : sources files for the library
26+
# PUBLIC_LIBRARIES: targets and flags for linking phase
27+
# PRIVATE_COMPILE_FLAGS: compile flags for the library. Will not be exported.
28+
# EXPORT_NAME: export name for the absl:: target export
29+
# TARGET: target name
30+
#
31+
# create a target associated to <NAME>
32+
# libraries are installed under CMAKE_INSTALL_FULL_LIBDIR by default
33+
#
34+
function(absl_library)
35+
cmake_parse_arguments(ABSL_LIB
36+
"DISABLE_INSTALL" # keep that in case we want to support installation one day
37+
"TARGET;EXPORT_NAME"
38+
"SOURCES;PUBLIC_LIBRARIES;PRIVATE_COMPILE_FLAGS;PUBLIC_INCLUDE_DIRS;PRIVATE_INCLUDE_DIRS"
39+
${ARGN}
40+
)
41+
42+
set(_NAME ${ABSL_LIB_TARGET})
43+
string(TOUPPER ${_NAME} _UPPER_NAME)
44+
45+
add_library(${_NAME} STATIC ${ABSL_LIB_SOURCES})
46+
47+
target_compile_options(${_NAME} PRIVATE ${ABSL_COMPILE_CXXFLAGS} ${ABSL_LIB_PRIVATE_COMPILE_FLAGS})
48+
target_link_libraries(${_NAME} PUBLIC ${ABSL_LIB_PUBLIC_LIBRARIES})
49+
target_include_directories(${_NAME}
50+
PUBLIC ${ABSL_COMMON_INCLUDE_DIRS} ${ABSL_LIB_PUBLIC_INCLUDE_DIRS}
51+
PRIVATE ${ABSL_LIB_PRIVATE_INCLUDE_DIRS}
52+
)
53+
54+
if(ABSL_LIB_EXPORT_NAME)
55+
add_library(absl::${ABSL_LIB_EXPORT_NAME} ALIAS ${_NAME})
56+
endif()
57+
endfunction()
58+
59+
60+
61+
#
62+
# header only virtual target creation
63+
#
64+
function(absl_header_library)
65+
cmake_parse_arguments(ABSL_HO_LIB
66+
"DISABLE_INSTALL"
67+
"EXPORT_NAME;TARGET"
68+
"PUBLIC_LIBRARIES;PRIVATE_COMPILE_FLAGS;PUBLIC_INCLUDE_DIRS;PRIVATE_INCLUDE_DIRS"
69+
${ARGN}
70+
)
71+
72+
set(_NAME ${ABSL_HO_LIB_TARGET})
73+
74+
set(__dummy_header_only_lib_file "${CMAKE_CURRENT_BINARY_DIR}/${_NAME}_header_only_dummy.cc")
75+
76+
if(NOT EXISTS ${__dummy_header_only_lib_file})
77+
file(WRITE ${__dummy_header_only_lib_file}
78+
"\
79+
/* generated file for header-only cmake target */ \
80+
\
81+
// single meaningless symbol \
82+
void __${_NAME}__header_fakesym() {} \
83+
\
84+
"
85+
)
86+
endif()
87+
88+
89+
add_library(${_NAME} ${__dummy_header_only_lib_file})
90+
target_link_libraries(${_NAME} PUBLIC ${ABSL_HO_LIB_PUBLIC_LIBRARIES})
91+
target_include_directories(${_NAME}
92+
PUBLIC ${ABSL_COMMON_INCLUDE_DIRS} ${ABSL_HO_LIB_PUBLIC_INCLUDE_DIRS}
93+
PRIVATE ${ABSL_HO_LIB_PRIVATE_INCLUDE_DIRS}
94+
)
95+
96+
if(ABSL_HO_LIB_EXPORT_NAME)
97+
add_library(absl::${ABSL_HO_LIB_EXPORT_NAME} ALIAS ${_NAME})
98+
endif()
99+
100+
endfunction()
101+
102+
103+
#
104+
# create an abseil unit_test and add it to the executed test list
105+
#
106+
# parameters
107+
# TARGET: target name prefix
108+
# SOURCES: sources files for the tests
109+
# PUBLIC_LIBRARIES: targets and flags for linking phase.
110+
# PRIVATE_COMPILE_FLAGS: compile flags for the test. Will not be exported.
111+
#
112+
# create a target associated to <NAME>_bin
113+
#
114+
# all tests will be register for execution with add_test()
115+
#
116+
# test compilation and execution is disable when BUILD_TESTING=OFF
117+
#
118+
function(absl_test)
119+
120+
cmake_parse_arguments(ABSL_TEST
121+
""
122+
"TARGET"
123+
"SOURCES;PUBLIC_LIBRARIES;PRIVATE_COMPILE_FLAGS;PUBLIC_INCLUDE_DIRS"
124+
${ARGN}
125+
)
126+
127+
128+
if(BUILD_TESTING)
129+
130+
set(_NAME ${ABSL_TEST_TARGET})
131+
string(TOUPPER ${_NAME} _UPPER_NAME)
132+
133+
add_executable(${_NAME}_bin ${ABSL_TEST_SOURCES})
134+
135+
target_compile_options(${_NAME}_bin PRIVATE ${ABSL_COMPILE_CXXFLAGS} ${ABSL_TEST_PRIVATE_COMPILE_FLAGS})
136+
target_link_libraries(${_NAME}_bin PUBLIC ${ABSL_TEST_PUBLIC_LIBRARIES} ${ABSL_TEST_COMMON_LIBRARIES})
137+
target_include_directories(${_NAME}_bin
138+
PUBLIC ${ABSL_COMMON_INCLUDE_DIRS} ${ABSL_TEST_PUBLIC_INCLUDE_DIRS}
139+
PRIVATE ${GMOCK_INCLUDE_DIRS} ${GTEST_INCLUDE_DIRS}
140+
)
141+
142+
add_test(${_NAME}_test ${_NAME}_bin)
143+
endif(BUILD_TESTING)
144+
145+
endfunction()
146+
147+
148+
149+
150+
function(check_target my_target)
151+
152+
if(NOT TARGET ${my_target})
153+
message(FATAL_ERROR " ABSL: compiling absl requires a ${my_target} CMake target in your project,
154+
see CMake/README.md for more details")
155+
endif(NOT TARGET ${my_target})
156+
157+
endfunction()

CMake/README.md

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
## Abseil CMake build instructions
3+
4+
5+
### Recommended usage : incorporate Abseil into an CMake project
6+
7+
We recommended to build and use abseil in the same way than googletest
8+
( https://github.com/google/googletest/blob/master/googletest/README.md )
9+
10+
* Download abseil and copy it in a sub-directory in your project.
11+
12+
* Or add abseil as a git-submodule in your project
13+
14+
You can then use the cmake `add_subdirectory()` command to include
15+
abseil directly and use the abseil targets in your project.
16+
17+
Abseil requires CCTZ and the googletest framework. Consequently,
18+
the targets `gtest`, `gtest_main`, `gmock` and `cctz` need
19+
to be declared in your project before including abseil with `add_subdirectory`.
20+
You can find instructions on how to get and build these projects at these
21+
URL :
22+
* cctz https://github.com/google/cctz
23+
* googletest https://github.com/google/googletest
24+
25+
26+
27+
Here is a short CMakeLists.txt example of a possible project file
28+
using abseil
29+
30+
project(my_project)
31+
32+
add_subdirectory(googletest)
33+
add_subdirectory(cctz)
34+
add_subdirectory(abseil-cpp)
35+
36+
add_executable(my_exe source.cpp)
37+
target_link_libraries(my_exe base synchronization strings)
38+
39+
40+
41+

CMakeLists.txt

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#
2+
# Copyright 2017 The Abseil Authors.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
cmake_minimum_required(VERSION 2.8.12)
17+
project(absl)
18+
19+
# enable ctest
20+
include(CTest)
21+
22+
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/CMake)
23+
24+
include(GNUInstallDirs)
25+
include(AbseilHelpers)
26+
27+
28+
# config options
29+
set(ABSL_STD_CXX_FLAG "-std=c++11" CACHE STRING "c++ std flag (default: c++11)")
30+
31+
32+
33+
##
34+
## Using absl targets
35+
##
36+
## all public absl targets are
37+
## exported with the absl:: prefix
38+
##
39+
## e.g absl::base absl::synchronization absl::strings ....
40+
##
41+
## DO NOT rely on the internal targets outside of the prefix
42+
43+
44+
# include current path
45+
list(APPEND ABSL_COMMON_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR})
46+
47+
# -std=X
48+
set(CMAKE_CXX_FLAGS "${ABSL_STD_CXX_FLAG} ${CMAKE_CXX_FLAGS}")
49+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_WARNING_VLA} ${CMAKE_CXX_FLAGS} ")
50+
51+
52+
# find dependencies
53+
## pthread
54+
find_package(Threads REQUIRED)
55+
56+
# commented: used only for standalone test
57+
#add_subdirectory(cctz)
58+
#add_subdirectory(googletest)
59+
60+
## check targets
61+
check_target(cctz)
62+
check_target(gtest)
63+
check_target(gtest_main)
64+
check_target(gmock)
65+
66+
# -fexceptions
67+
set(ABSL_EXCEPTIONS_FLAG "${CMAKE_CXX_EXCEPTIONS}")
68+
69+
# fix stuff
70+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FIX_MSVC} ${CMAKE_CXX_FLAGS}")
71+
72+
list(APPEND ABSL_TEST_COMMON_LIBRARIES
73+
gtest_main
74+
gtest
75+
gmock
76+
${CMAKE_THREAD_LIBS_INIT}
77+
)
78+
79+
add_subdirectory(absl)
80+
81+

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ standard library.
1010
- [Codemap](#codemap)
1111
- [License](#license)
1212
- [Links](#links)
13+
- [Build with cmake](#cmake)
1314

1415
<a name="about"></a>
1516
## About Abseil
@@ -85,6 +86,11 @@ For more information about Abseil:
8586
[Abseil Compatibility Guarantees](http://abseil.io/about/compatibility) to
8687
understand both what we promise to you, and what we expect of you in return.
8788

89+
<a name="cmake"></a>
90+
## Build with CMake
91+
92+
Please check the [CMake build instructions](CMake/README.md)
93+
8894
## Disclaimer
8995

9096
* This is not an official Google product.

absl/CMakeLists.txt

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#
2+
# Copyright 2017 The Abseil Authors.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
18+
19+
add_subdirectory(base)
20+
add_subdirectory(algorithm)
21+
add_subdirectory(container)
22+
add_subdirectory(debugging)
23+
add_subdirectory(memory)
24+
add_subdirectory(meta)
25+
add_subdirectory(numeric)
26+
add_subdirectory(strings)
27+
add_subdirectory(synchronization)
28+
add_subdirectory(time)
29+
add_subdirectory(types)
30+
add_subdirectory(utility)

absl/algorithm/CMakeLists.txt

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#
2+
# Copyright 2017 The Abseil Authors.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
list(APPEND ALGORITHM_PUBLIC_HEADERS
18+
"algorithm.h"
19+
"container.h"
20+
)
21+
22+
23+
#
24+
## TESTS
25+
#
26+
27+
# test algorithm_test
28+
list(APPEND ALGORITHM_TEST_SRC
29+
"algorithm_test.cc"
30+
${ALGORITHM_PUBLIC_HEADERS}
31+
${ALGORITHM_INTERNAL_HEADERS}
32+
)
33+
34+
absl_header_library(
35+
TARGET
36+
absl_algorithm
37+
EXPORT_NAME
38+
algorithm
39+
)
40+
41+
absl_test(
42+
TARGET
43+
algorithm_test
44+
SOURCES
45+
${ALGORITHM_TEST_SRC}
46+
PUBLIC_LIBRARIES
47+
absl::algorithm
48+
)
49+
50+
51+
52+
53+
# test container_test
54+
set(CONTAINER_TEST_SRC "container_test.cc")
55+
56+
absl_test(
57+
TARGET
58+
container_test
59+
SOURCES
60+
${CONTAINER_TEST_SRC}
61+
PUBLIC_LIBRARIES
62+
absl::algorithm
63+
)

0 commit comments

Comments
 (0)