forked from abseil/abseil-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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
- Loading branch information
Showing
17 changed files
with
1,766 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
# | ||
# Copyright 2017 The Abseil Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
include(CMakeParseArguments) | ||
|
||
set(_ABSL_HELPERS_PATH "${CMAKE_CURRENT_LIST_DIR}") | ||
|
||
# | ||
# create a static library absl_based on the following variable | ||
# | ||
# parameters | ||
# SOURCES : sources files for the library | ||
# PUBLIC_LIBRARIES: targets and flags for linking phase | ||
# PRIVATE_COMPILE_FLAGS: compile flags for the library. Will not be exported. | ||
# EXPORT_NAME: export name for the absl:: target export | ||
# TARGET: target name | ||
# | ||
# create a target associated to <NAME> | ||
# libraries are installed under CMAKE_INSTALL_FULL_LIBDIR by default | ||
# | ||
function(absl_library) | ||
cmake_parse_arguments(ABSL_LIB | ||
"DISABLE_INSTALL" # keep that in case we want to support installation one day | ||
"TARGET;EXPORT_NAME" | ||
"SOURCES;PUBLIC_LIBRARIES;PRIVATE_COMPILE_FLAGS;PUBLIC_INCLUDE_DIRS;PRIVATE_INCLUDE_DIRS" | ||
${ARGN} | ||
) | ||
|
||
set(_NAME ${ABSL_LIB_TARGET}) | ||
string(TOUPPER ${_NAME} _UPPER_NAME) | ||
|
||
add_library(${_NAME} STATIC ${ABSL_LIB_SOURCES}) | ||
|
||
target_compile_options(${_NAME} PRIVATE ${ABSL_COMPILE_CXXFLAGS} ${ABSL_LIB_PRIVATE_COMPILE_FLAGS}) | ||
target_link_libraries(${_NAME} PUBLIC ${ABSL_LIB_PUBLIC_LIBRARIES}) | ||
target_include_directories(${_NAME} | ||
PUBLIC ${ABSL_COMMON_INCLUDE_DIRS} ${ABSL_LIB_PUBLIC_INCLUDE_DIRS} | ||
PRIVATE ${ABSL_LIB_PRIVATE_INCLUDE_DIRS} | ||
) | ||
|
||
if(ABSL_LIB_EXPORT_NAME) | ||
add_library(absl::${ABSL_LIB_EXPORT_NAME} ALIAS ${_NAME}) | ||
endif() | ||
endfunction() | ||
|
||
|
||
|
||
# | ||
# header only virtual target creation | ||
# | ||
function(absl_header_library) | ||
cmake_parse_arguments(ABSL_HO_LIB | ||
"DISABLE_INSTALL" | ||
"EXPORT_NAME;TARGET" | ||
"PUBLIC_LIBRARIES;PRIVATE_COMPILE_FLAGS;PUBLIC_INCLUDE_DIRS;PRIVATE_INCLUDE_DIRS" | ||
${ARGN} | ||
) | ||
|
||
set(_NAME ${ABSL_HO_LIB_TARGET}) | ||
|
||
set(__dummy_header_only_lib_file "${CMAKE_CURRENT_BINARY_DIR}/${_NAME}_header_only_dummy.cc") | ||
|
||
if(NOT EXISTS ${__dummy_header_only_lib_file}) | ||
file(WRITE ${__dummy_header_only_lib_file} | ||
"\ | ||
/* generated file for header-only cmake target */ \ | ||
\ | ||
// single meaningless symbol \ | ||
void __${_NAME}__header_fakesym() {} \ | ||
\ | ||
" | ||
) | ||
endif() | ||
|
||
|
||
add_library(${_NAME} ${__dummy_header_only_lib_file}) | ||
target_link_libraries(${_NAME} PUBLIC ${ABSL_HO_LIB_PUBLIC_LIBRARIES}) | ||
target_include_directories(${_NAME} | ||
PUBLIC ${ABSL_COMMON_INCLUDE_DIRS} ${ABSL_HO_LIB_PUBLIC_INCLUDE_DIRS} | ||
PRIVATE ${ABSL_HO_LIB_PRIVATE_INCLUDE_DIRS} | ||
) | ||
|
||
if(ABSL_HO_LIB_EXPORT_NAME) | ||
add_library(absl::${ABSL_HO_LIB_EXPORT_NAME} ALIAS ${_NAME}) | ||
endif() | ||
|
||
endfunction() | ||
|
||
|
||
# | ||
# create an abseil unit_test and add it to the executed test list | ||
# | ||
# parameters | ||
# TARGET: target name prefix | ||
# SOURCES: sources files for the tests | ||
# PUBLIC_LIBRARIES: targets and flags for linking phase. | ||
# PRIVATE_COMPILE_FLAGS: compile flags for the test. Will not be exported. | ||
# | ||
# create a target associated to <NAME>_bin | ||
# | ||
# all tests will be register for execution with add_test() | ||
# | ||
# test compilation and execution is disable when BUILD_TESTING=OFF | ||
# | ||
function(absl_test) | ||
|
||
cmake_parse_arguments(ABSL_TEST | ||
"" | ||
"TARGET" | ||
"SOURCES;PUBLIC_LIBRARIES;PRIVATE_COMPILE_FLAGS;PUBLIC_INCLUDE_DIRS" | ||
${ARGN} | ||
) | ||
|
||
|
||
if(BUILD_TESTING) | ||
|
||
set(_NAME ${ABSL_TEST_TARGET}) | ||
string(TOUPPER ${_NAME} _UPPER_NAME) | ||
|
||
add_executable(${_NAME}_bin ${ABSL_TEST_SOURCES}) | ||
|
||
target_compile_options(${_NAME}_bin PRIVATE ${ABSL_COMPILE_CXXFLAGS} ${ABSL_TEST_PRIVATE_COMPILE_FLAGS}) | ||
target_link_libraries(${_NAME}_bin PUBLIC ${ABSL_TEST_PUBLIC_LIBRARIES} ${ABSL_TEST_COMMON_LIBRARIES}) | ||
target_include_directories(${_NAME}_bin | ||
PUBLIC ${ABSL_COMMON_INCLUDE_DIRS} ${ABSL_TEST_PUBLIC_INCLUDE_DIRS} | ||
PRIVATE ${GMOCK_INCLUDE_DIRS} ${GTEST_INCLUDE_DIRS} | ||
) | ||
|
||
add_test(${_NAME}_test ${_NAME}_bin) | ||
endif(BUILD_TESTING) | ||
|
||
endfunction() | ||
|
||
|
||
|
||
|
||
function(check_target my_target) | ||
|
||
if(NOT TARGET ${my_target}) | ||
message(FATAL_ERROR " ABSL: compiling absl requires a ${my_target} CMake target in your project, | ||
see CMake/README.md for more details") | ||
endif(NOT TARGET ${my_target}) | ||
|
||
endfunction() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
|
||
## Abseil CMake build instructions | ||
|
||
|
||
### Recommended usage : incorporate Abseil into an CMake project | ||
|
||
We recommended to build and use abseil in the same way than googletest | ||
( https://github.com/google/googletest/blob/master/googletest/README.md ) | ||
|
||
* Download abseil and copy it in a sub-directory in your project. | ||
|
||
* Or add abseil as a git-submodule in your project | ||
|
||
You can then use the cmake `add_subdirectory()` command to include | ||
abseil directly and use the abseil targets in your project. | ||
|
||
Abseil requires CCTZ and the googletest framework. Consequently, | ||
the targets `gtest`, `gtest_main`, `gmock` and `cctz` need | ||
to be declared in your project before including abseil with `add_subdirectory`. | ||
You can find instructions on how to get and build these projects at these | ||
URL : | ||
* cctz https://github.com/google/cctz | ||
* googletest https://github.com/google/googletest | ||
|
||
|
||
|
||
Here is a short CMakeLists.txt example of a possible project file | ||
using abseil | ||
|
||
project(my_project) | ||
|
||
add_subdirectory(googletest) | ||
add_subdirectory(cctz) | ||
add_subdirectory(abseil-cpp) | ||
|
||
add_executable(my_exe source.cpp) | ||
target_link_libraries(my_exe base synchronization strings) | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# | ||
# Copyright 2017 The Abseil Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
cmake_minimum_required(VERSION 2.8.12) | ||
project(absl) | ||
|
||
# enable ctest | ||
include(CTest) | ||
|
||
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/CMake) | ||
|
||
include(GNUInstallDirs) | ||
include(AbseilHelpers) | ||
|
||
|
||
# config options | ||
set(ABSL_STD_CXX_FLAG "-std=c++11" CACHE STRING "c++ std flag (default: c++11)") | ||
|
||
|
||
|
||
## | ||
## Using absl targets | ||
## | ||
## all public absl targets are | ||
## exported with the absl:: prefix | ||
## | ||
## e.g absl::base absl::synchronization absl::strings .... | ||
## | ||
## DO NOT rely on the internal targets outside of the prefix | ||
|
||
|
||
# include current path | ||
list(APPEND ABSL_COMMON_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}) | ||
|
||
# -std=X | ||
set(CMAKE_CXX_FLAGS "${ABSL_STD_CXX_FLAG} ${CMAKE_CXX_FLAGS}") | ||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_WARNING_VLA} ${CMAKE_CXX_FLAGS} ") | ||
|
||
|
||
# find dependencies | ||
## pthread | ||
find_package(Threads REQUIRED) | ||
|
||
# commented: used only for standalone test | ||
#add_subdirectory(cctz) | ||
#add_subdirectory(googletest) | ||
|
||
## check targets | ||
check_target(cctz) | ||
check_target(gtest) | ||
check_target(gtest_main) | ||
check_target(gmock) | ||
|
||
# -fexceptions | ||
set(ABSL_EXCEPTIONS_FLAG "${CMAKE_CXX_EXCEPTIONS}") | ||
|
||
# fix stuff | ||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FIX_MSVC} ${CMAKE_CXX_FLAGS}") | ||
|
||
list(APPEND ABSL_TEST_COMMON_LIBRARIES | ||
gtest_main | ||
gtest | ||
gmock | ||
${CMAKE_THREAD_LIBS_INIT} | ||
) | ||
|
||
add_subdirectory(absl) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# | ||
# Copyright 2017 The Abseil Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
|
||
|
||
add_subdirectory(base) | ||
add_subdirectory(algorithm) | ||
add_subdirectory(container) | ||
add_subdirectory(debugging) | ||
add_subdirectory(memory) | ||
add_subdirectory(meta) | ||
add_subdirectory(numeric) | ||
add_subdirectory(strings) | ||
add_subdirectory(synchronization) | ||
add_subdirectory(time) | ||
add_subdirectory(types) | ||
add_subdirectory(utility) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# | ||
# Copyright 2017 The Abseil Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
list(APPEND ALGORITHM_PUBLIC_HEADERS | ||
"algorithm.h" | ||
"container.h" | ||
) | ||
|
||
|
||
# | ||
## TESTS | ||
# | ||
|
||
# test algorithm_test | ||
list(APPEND ALGORITHM_TEST_SRC | ||
"algorithm_test.cc" | ||
${ALGORITHM_PUBLIC_HEADERS} | ||
${ALGORITHM_INTERNAL_HEADERS} | ||
) | ||
|
||
absl_header_library( | ||
TARGET | ||
absl_algorithm | ||
EXPORT_NAME | ||
algorithm | ||
) | ||
|
||
absl_test( | ||
TARGET | ||
algorithm_test | ||
SOURCES | ||
${ALGORITHM_TEST_SRC} | ||
PUBLIC_LIBRARIES | ||
absl::algorithm | ||
) | ||
|
||
|
||
|
||
|
||
# test container_test | ||
set(CONTAINER_TEST_SRC "container_test.cc") | ||
|
||
absl_test( | ||
TARGET | ||
container_test | ||
SOURCES | ||
${CONTAINER_TEST_SRC} | ||
PUBLIC_LIBRARIES | ||
absl::algorithm | ||
) |
Oops, something went wrong.