From 3695da5fbd25aa9367cace40382270c9988dbaf6 Mon Sep 17 00:00:00 2001 From: David Neto Date: Sun, 26 Mar 2017 09:58:42 -0400 Subject: Initial commit of Effcee --- cmake/linux-mingw-toolchain.cmake | 35 +++++++++++++++++ cmake/setup_build.cmake | 83 +++++++++++++++++++++++++++++++++++++++ cmake/utils.cmake | 62 +++++++++++++++++++++++++++++ 3 files changed, 180 insertions(+) create mode 100644 cmake/linux-mingw-toolchain.cmake create mode 100644 cmake/setup_build.cmake create mode 100644 cmake/utils.cmake (limited to 'cmake') diff --git a/cmake/linux-mingw-toolchain.cmake b/cmake/linux-mingw-toolchain.cmake new file mode 100644 index 0000000..9f7f676 --- /dev/null +++ b/cmake/linux-mingw-toolchain.cmake @@ -0,0 +1,35 @@ +# Copyright 2017 The Effcee 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. + +SET(CMAKE_SYSTEM_NAME Windows) + +set(MINGW_COMPILER_PREFIX "i686-w64-mingw32" CACHE STRING + "What compiler prefix to use for mingw") + +set(MINGW_SYSROOT "/usr/${MINGW_COMPILER_PREFIX}" CACHE STRING + "What sysroot to use for mingw") + +# Which compilers to use for C and C++ +find_program(CMAKE_RC_COMPILER NAMES ${MINGW_COMPILER_PREFIX}-windres) +find_program(CMAKE_C_COMPILER NAMES ${MINGW_COMPILER_PREFIX}-gcc) +find_program(CMAKE_CXX_COMPILER NAMES ${MINGW_COMPILER_PREFIX}-g++) + +SET(CMAKE_FIND_ROOT_PATH ${MINGW_SYSROOT}) + +# Adjust the default behaviour of the FIND_XXX() commands: +# Search headers and libraries in the target environment; search +# programs in the host environment. +set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) +set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) +set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) diff --git a/cmake/setup_build.cmake b/cmake/setup_build.cmake new file mode 100644 index 0000000..40749fc --- /dev/null +++ b/cmake/setup_build.cmake @@ -0,0 +1,83 @@ +# Copyright 2017 The Effcee 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. + +# For cross-compilation, we need to use find_host_package +# in the remaining setup. But if not cross-compiling, then we +# need to alias find_host_package to find_package. +# Similar for find_host_program. +if(NOT COMMAND find_host_package) + macro(find_host_package) + find_package(${ARGN}) + endmacro() +endif() +if(NOT COMMAND find_host_program) + macro(find_host_program) + find_program(${ARGN}) + endmacro() +endif() + +if (ANDROID) + # For android let's preemptively find the correct packages so that + # child projects (e.g. googletest) do not fail to find them. + find_host_package(PythonInterp) +endif() + +foreach(PROGRAM echo python) + string(TOUPPER ${PROGRAM} PROG_UC) + if (ANDROID) + find_host_program(${PROG_UC}_EXE ${PROGRAM} REQUIRED) + else() + find_program(${PROG_UC}_EXE ${PROGRAM} REQUIRED) + endif() +endforeach(PROGRAM) + +option(DISABLE_RTTI "Disable RTTI in builds") +if(DISABLE_RTTI) + if(UNIX) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti") + endif(UNIX) +endif(DISABLE_RTTI) + +option(DISABLE_EXCEPTIONS "Disables exceptions in builds") +if(DISABLE_EXCEPTIONS) + if(UNIX) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions") + endif(UNIX) +endif(DISABLE_EXCEPTIONS) + +if(WIN32) + # Ensure that gmock compiles the same as the rest of the code, otherwise + # failures will occur. + set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) +endif(WIN32) + +if(WIN32) +# On Windows, CMake by default compiles with the shared CRT. +# Default it to the static CRT. + option(EFFCEE_ENABLE_SHARED_CRT + "Use the shared CRT with MSVC instead of the static CRT" + ${EFFCEE_ENABLE_SHARED_CRT}) + if (NOT EFFCEE_ENABLE_SHARED_CRT) + if(MSVC) + # Link executables statically by replacing /MD with /MT everywhere. + foreach(flag_var + CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE + CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO) + if(${flag_var} MATCHES "/MD") + string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}") + endif(${flag_var} MATCHES "/MD") + endforeach(flag_var) + endif(MSVC) + endif(NOT EFFCEE_ENABLE_SHARED_CRT) +endif(WIN32) diff --git a/cmake/utils.cmake b/cmake/utils.cmake new file mode 100644 index 0000000..727b7ea --- /dev/null +++ b/cmake/utils.cmake @@ -0,0 +1,62 @@ +# Copyright 2017 The Effcee 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. + +# Utility functions + +function(effcee_default_c_compile_options TARGET) + if (NOT "${MSVC}") + target_compile_options(${TARGET} PRIVATE -Wall -Werror) + if (UNIX) + target_compile_options(${TARGET} PUBLIC -pthread) + endif(UNIX) + if (ENABLE_CODE_COVERAGE) + # The --coverage option is a synonym for -fprofile-arcs -ftest-coverage + # when compiling. + target_compile_options(${TARGET} PRIVATE -g -O0 --coverage) + # The --coverage option is a synonym for -lgcov when linking for gcc. + # For clang, it links in a different library, libclang_rt.profile, which + # requires clang to be built with compiler-rt. + target_link_libraries(${TARGET} PRIVATE --coverage) + endif() + if (NOT EFFCEE_ENABLE_SHARED_CRT) + if (WIN32) + # For MinGW cross compile, statically link to the libgcc runtime. + # But it still depends on MSVCRT.dll. + set_target_properties(${TARGET} PROPERTIES + LINK_FLAGS "-static -static-libgcc") + endif(WIN32) + endif(NOT EFFCEE_ENABLE_SHARED_CRT) + target_link_libraries(${TARGET} PUBLIC ${CMAKE_THREAD_LIBS_INIT}) + else() + # disable warning C4800: 'int' : forcing value to bool 'true' or 'false' + # (performance warning) + target_compile_options(${TARGET} PRIVATE /wd4800) + endif() +endfunction(effcee_default_c_compile_options) + +function(effcee_default_compile_options TARGET) + effcee_default_c_compile_options(${TARGET}) + if (NOT "${MSVC}") + # RE2's public header requires C++11. So publicly required C++11 + target_compile_options(${TARGET} PUBLIC -std=c++11) + if (NOT EFFCEE_ENABLE_SHARED_CRT) + if (WIN32) + # For MinGW cross compile, statically link to the C++ runtime. + # But it still depends on MSVCRT.dll. + set_target_properties(${TARGET} PROPERTIES + LINK_FLAGS "-static -static-libgcc -static-libstdc++") + endif(WIN32) + endif(NOT EFFCEE_ENABLE_SHARED_CRT) + endif() +endfunction(effcee_default_compile_options) -- cgit v1.2.3