[PATCH] Add a cmake build system.

Dan Albert danalbert at google.com
Thu Jun 26 23:43:22 PDT 2014


No longer have to perform a full rebuild every time. This also makes
building on Linux simpler, and allows building libcxxabi to be built
from within the llvm tree (under projects/).

This does not yet support building the unwinder in src/Unwind. That will
be added in a later patch. Will also add support for tests with lit in a
later patch.
---
 CMakeLists.txt        | 214 ++++++++++++++++++++++++++++++++++++++++++++++++++
 cmake/config-ix.cmake |  27 +++++++
 src/CMakeLists.txt    |  74 +++++++++++++++++
 www/index.html        |  10 +++
 4 files changed, 325 insertions(+)
 create mode 100644 CMakeLists.txt
 create mode 100644 cmake/config-ix.cmake
 create mode 100644 src/CMakeLists.txt

diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 0000000..26c4d91
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,214 @@
+#===============================================================================
+# Setup Project
+#===============================================================================
+
+cmake_minimum_required(VERSION 2.8.11)
+
+if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
+  project(libcxxabi)
+
+  # Rely on llvm-config.
+  set(CONFIG_OUTPUT)
+  find_program(LLVM_CONFIG "llvm-config")
+  if(LLVM_CONFIG)
+    message(STATUS "Found LLVM_CONFIG as ${LLVM_CONFIG}")
+    set(CONFIG_COMMAND ${LLVM_CONFIG}
+      "--bindir"
+      "--libdir"
+      "--prefix"
+      "--src-root")
+    execute_process(
+      COMMAND ${CONFIG_COMMAND}
+      RESULT_VARIABLE HAD_ERROR
+      OUTPUT_VARIABLE CONFIG_OUTPUT
+    )
+    if(NOT HAD_ERROR)
+      string(REGEX REPLACE
+        "[ \t]*[\r\n]+[ \t]*" ";"
+        CONFIG_OUTPUT ${CONFIG_OUTPUT})
+    else()
+      string(REPLACE ";" " " CONFIG_COMMAND_STR "${CONFIG_COMMAND}")
+      message(STATUS "${CONFIG_COMMAND_STR}")
+      message(FATAL_ERROR "llvm-config failed with status ${HAD_ERROR}")
+    endif()
+  else()
+    message(FATAL_ERROR "llvm-config not found -- ${LLVM_CONFIG}")
+  endif()
+
+  list(GET CONFIG_OUTPUT 0 TOOLS_BINARY_DIR)
+  list(GET CONFIG_OUTPUT 1 LIBRARY_DIR)
+  list(GET CONFIG_OUTPUT 2 LLVM_OBJ_ROOT)
+  list(GET CONFIG_OUTPUT 3 MAIN_SRC_DIR)
+
+  set(LLVM_TOOLS_BINARY_DIR ${TOOLS_BINARY_DIR} CACHE PATH "Path to llvm/bin")
+  set(LLVM_LIBRARY_DIR ${LIBRARY_DIR} CACHE PATH "Path to llvm/lib")
+  set(LLVM_BINARY_DIR ${LLVM_OBJ_ROOT} CACHE PATH "Path to LLVM build tree")
+  set(LLVM_MAIN_SRC_DIR ${MAIN_SRC_DIR} CACHE PATH "Path to LLVM source tree")
+
+  set(LLVM_CMAKE_PATH "${LLVM_BINARY_DIR}/share/llvm/cmake")
+  set(LLVMCONFIG_FILE "${LLVM_CMAKE_PATH}/LLVMConfig.cmake")
+  if(EXISTS ${LLVMCONFIG_FILE})
+    list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_PATH}")
+    include(${LLVMCONFIG_FILE})
+  else()
+    message(FATAL_ERROR "Not found: ${LLVMCONFIG_FILE}")
+  endif()
+
+  set(PACKAGE_NAME libcxxabi)
+  set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}")
+  set(PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}")
+  set(PACKAGE_BUGREPORT "llvmbugs at cs.uiuc.edu")
+
+  if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
+    set(LLVM_LIT ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
+  else()
+    # Seek installed Lit.
+    find_program(LLVM_LIT "lit.py" ${LLVM_MAIN_SRC_DIR}/utils/lit
+      DOC "Path to lit.py")
+  endif()
+
+  if(LLVM_LIT)
+    # Define the default arguments to use with 'lit', and an option for the user
+    # to override.
+    set(LIT_ARGS_DEFAULT "-sv")
+    if (MSVC OR XCODE)
+      set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
+    endif()
+    set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
+
+    # On Win32 hosts, provide an option to specify the path to the GnuWin32 tools.
+    if( WIN32 AND NOT CYGWIN )
+      set(LLVM_LIT_TOOLS_DIR "" CACHE PATH "Path to GnuWin32 tools")
+    endif()
+  else()
+    set(LLVM_INCLUDE_TESTS OFF)
+  endif()
+
+  set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
+  set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
+
+  set(LIBCXXABI_BUILT_STANDALONE 1)
+endif()
+
+#===============================================================================
+# Setup CMake Options
+#===============================================================================
+
+# Define options.
+option(LIBCXXABI_ENABLE_RTTI "Use run time type information." ON)
+option(LIBCXXABI_ENABLE_ASSERTIONS "Enable assertions independent of build mode." ON)
+option(LIBCXXABI_ENABLE_PEDANTIC "Compile with pedantic enabled." ON)
+option(LIBCXXABI_ENABLE_WERROR "Fail and stop if a warning is triggered." OFF)
+option(LIBCXXABI_ENABLE_CXX11 "Enable -std=c++11 and use of c++11 language features if the compiler supports it." ON)
+option(LIBCXXABI_ENABLE_SHARED "Build libc++abi as a shared library." ON)
+
+#===============================================================================
+# Configure System
+#===============================================================================
+
+# Add path for custom modules
+set(CMAKE_MODULE_PATH
+  "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
+  "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules"
+  ${CMAKE_MODULE_PATH}
+  )
+
+# Configure compiler.
+include(config-ix)
+
+#===============================================================================
+# Setup Compiler Flags
+#===============================================================================
+
+# Get required flags.
+macro(append_if list condition var)
+  if (${condition})
+    list(APPEND ${list} ${var})
+  endif()
+endmacro()
+
+if (LIBCXXABI_HAS_NOSTDINCXX_FLAG)
+  list(APPEND LIBCXXABI_CXX_REQUIRED_FLAGS -nostdinc++)
+endif()
+if (LIBCXXABI_ENABLE_CXX11 AND LIBCXXABI_HAS_STDCXX11_FLAG)
+  list(APPEND LIBCXXABI_CXX_REQUIRED_FLAGS -std=c++11)
+endif()
+
+# Get warning flags
+if (NOT MSVC)
+  append_if(LIBCXXABI_CXX_WARNING_FLAGS LIBCXXABI_HAS_WALL_FLAG -Wall)
+  append_if(LIBCXXABI_CXX_WARNING_FLAGS LIBCXXABI_HAS_WNO_UNUSED_FUNCTION_FLAG -Wno-unused-function)
+  list(APPEND LIBCXXABI_CXX_REQUIRED_FLAGS -Werror=return-type)
+endif()
+
+append_if(LIBCXXABI_CXX_WARNING_FLAGS LIBCXXABI_HAS_W_FLAG -W)
+append_if(LIBCXXABI_CXX_WARNING_FLAGS LIBCXXABI_HAS_WNO_UNUSED_PARAMETER_FLAG -Wno-unused-parameter)
+append_if(LIBCXXABI_CXX_WARNING_FLAGS LIBCXXABI_HAS_WWRITE_STRINGS_FLAG -Wwrite-strings)
+append_if(LIBCXXABI_CXX_WARNING_FLAGS LIBCXXABI_HAS_WNO_LONG_LONG_FLAG -Wno-long-long)
+if (LIBCXXABI_ENABLE_WERROR)
+  append_if(LIBCXXABI_CXX_WARNING_FLAGS LIBCXXABI_HAS_WERROR_FLAG -Werror)
+  append_if(LIBCXXABI_CXX_WARNING_FLAGS LIBCXXABI_HAS_WX_FLAG -WX)
+else()
+  append_if(LIBCXXABI_CXX_WARNING_FLAGS LIBCXXABI_HAS_WNO_ERROR_FLAG -Wno-error)
+  append_if(LIBCXXABI_CXX_WARNING_FLAGS LIBCXXABI_HAS_NO_WX_FLAG -WX-)
+endif()
+if (LIBCXXABI_ENABLE_PEDANTIC)
+  append_if(LIBCXXABI_CXX_WARNING_FLAGS LIBCXXABI_HAS_PEDANTIC_FLAG -pedantic)
+endif()
+
+# Get feature flags.
+# Exceptions
+# Catches C++ exceptions only and tells the compiler to assume that extern C
+# functions never throw a C++ exception.
+append_if(LIBCXXABI_CXX_FEATURE_FLAGS LIBCXXABI_HAS_EHSC_FLAG -EHsc)
+# RTTI
+if (NOT LIBCXXABI_ENABLE_RTTI)
+  list(APPEND LIBCXXABI_CXX_FEATURE_FLAGS -D_LIBCPP_NO_RTTI)
+  append_if(LIBCXXABI_CXX_FEATURE_FLAGS LIBCXXABI_HAS_NO_GR_FLAG -GR-)
+  append_if(LIBCXXABI_CXX_FEATURE_FLAGS LIBCXXABI_HAS_FNO_RTTI_FLAG -fno-rtti)
+endif()
+# Assert
+string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE)
+if (LIBCXXABI_ENABLE_ASSERTIONS)
+  # MSVC doesn't like _DEBUG on release builds. See PR 4379.
+  if (NOT MSVC)
+    list(APPEND LIBCXXABI_CXX_FEATURE_FLAGS -D_DEBUG)
+  endif()
+  # On Release builds cmake automatically defines NDEBUG, so we
+  # explicitly undefine it:
+  if (uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE")
+    list(APPEND LIBCXXABI_CXX_FEATURE_FLAGS -UNDEBUG)
+  endif()
+else()
+  if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE")
+    list(APPEND LIBCXXABI_CXX_FEATURE_FLAGS -DNDEBUG)
+  endif()
+endif()
+# Static library
+if (NOT LIBCXXABI_ENABLE_SHARED)
+  list(APPEND LIBCXXABI_CXX_FEATURE_FLAGS -D_LIBCPP_BUILD_STATIC)
+endif()
+
+# This is the _ONLY_ place where add_definitions is called.
+if (MSVC)
+  add_definitions(-D_CRT_SECURE_NO_WARNINGS)
+endif()
+
+string(REPLACE ";" " " LIBCXXABI_CXX_REQUIRED_FLAGS "${LIBCXXABI_CXX_REQUIRED_FLAGS}")
+set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${LIBCXXABI_CXX_REQUIRED_FLAGS}")
+
+string(REPLACE ";" " " LIBCXXABI_CXX_WARNING_FLAGS "${LIBCXXABI_CXX_WARNING_FLAGS}")
+set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${LIBCXXABI_CXX_WARNING_FLAGS}")
+
+string(REPLACE ";" " " LIBCXXABI_CXX_FEATURE_FLAGS "${LIBCXXABI_CXX_FEATURE_FLAGS}")
+set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${LIBCXXABI_CXX_FEATURE_FLAGS}")
+
+#===============================================================================
+# Setup Source Code
+#===============================================================================
+
+include_directories(include)
+
+# Add source code. This also contains all of the logic for deciding linker flags
+# soname, etc...
+add_subdirectory(src)
diff --git a/cmake/config-ix.cmake b/cmake/config-ix.cmake
new file mode 100644
index 0000000..96da3f5
--- /dev/null
+++ b/cmake/config-ix.cmake
@@ -0,0 +1,27 @@
+include(CheckLibraryExists)
+include(CheckCXXCompilerFlag)
+
+# Check compiler flags
+check_cxx_compiler_flag(-std=c++11            LIBCXXABI_HAS_STDCXX11_FLAG)
+check_cxx_compiler_flag(-fPIC                 LIBCXXABI_HAS_FPIC_FLAG)
+check_cxx_compiler_flag(-nodefaultlibs        LIBCXXABI_HAS_NODEFAULTLIBS_FLAG)
+check_cxx_compiler_flag(-nostdinc++           LIBCXXABI_HAS_NOSTDINCXX_FLAG)
+check_cxx_compiler_flag(-Wall                 LIBCXXABI_HAS_WALL_FLAG)
+check_cxx_compiler_flag(-W                    LIBCXXABI_HAS_W_FLAG)
+check_cxx_compiler_flag(-Wno-unused-function  LIBCXXABI_HAS_WNO_UNUSED_FUNCTION_FLAG)
+check_cxx_compiler_flag(-Wno-unused-parameter LIBCXXABI_HAS_WNO_UNUSED_PARAMETER_FLAG)
+check_cxx_compiler_flag(-Wwrite-strings       LIBCXXABI_HAS_WWRITE_STRINGS_FLAG)
+check_cxx_compiler_flag(-Wno-long-long        LIBCXXABI_HAS_WNO_LONG_LONG_FLAG)
+check_cxx_compiler_flag(-pedantic             LIBCXXABI_HAS_PEDANTIC_FLAG)
+check_cxx_compiler_flag(-Werror               LIBCXXABI_HAS_WERROR_FLAG)
+check_cxx_compiler_flag(-Wno-error            LIBCXXABI_HAS_WNO_ERROR_FLAG)
+check_cxx_compiler_flag(-fno-rtti             LIBCXXABI_HAS_FNO_RTTI_FLAG)
+check_cxx_compiler_flag(/WX                   LIBCXXABI_HAS_WX_FLAG)
+check_cxx_compiler_flag(/WX-                  LIBCXXABI_HAS_NO_WX_FLAG)
+check_cxx_compiler_flag(/EHsc                 LIBCXXABI_HAS_EHSC_FLAG)
+check_cxx_compiler_flag(/EHs-                 LIBCXXABI_HAS_NO_EHS_FLAG)
+check_cxx_compiler_flag(/EHa-                 LIBCXXABI_HAS_NO_EHA_FLAG)
+check_cxx_compiler_flag(/GR-                  LIBCXXABI_HAS_NO_GR_FLAG)
+
+# Check libraries
+check_library_exists(c printf "" LIBCXXABI_HAS_C_LIB)
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
new file mode 100644
index 0000000..416f433
--- /dev/null
+++ b/src/CMakeLists.txt
@@ -0,0 +1,74 @@
+# Get sources
+file(GLOB LIBCXXABI_SOURCES *.cpp)
+
+# Add all the headers to the project for IDEs.
+if (MSVC_IDE OR XCODE)
+  file(GLOB_RECURSE LIBCXXABI_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/../include/*)
+  # Force them all into the headers dir on MSVC, otherwise they end up at
+  # project scope because they don't have extensions.
+  if (MSVC_IDE)
+    source_group("Header Files" FILES ${LIBCXXABI_HEADERS})
+  endif()
+endif()
+
+if (LIBCXXABI_ENABLE_SHARED)
+  add_library(cxxabi SHARED
+    ${LIBCXXABI_SOURCES}
+    ${LIBCXXABI_HEADERS}
+    )
+else()
+  add_library(cxxabi STATIC
+    ${LIBCXXABI_SOURCES}
+    ${LIBCXXABI_HEADERS}
+    )
+endif()
+
+if (LIBCXXABI_LIBCXX_PATH)
+  target_include_directories(cxxabi PRIVATE "${LIBCXXABI_LIBCXX_PATH}/include")
+elseif (NOT LIBCXXABI_BUILT_STANDALONE)
+  target_include_directories(cxxabi PRIVATE
+                             "${LLVM_MAIN_SRC_DIR}/projects/libcxx/include")
+elseif (NOT APPLE)
+  message(FATAL_ERROR "LIBCXXABI_LIBCXX_PATH is not set.")
+endif()
+
+# Generate library list.
+set(libraries ${LIBCXXABI_CXX_ABI_LIBRARIES})
+append_if(libraries LIBCXXABI_HAS_C_LIB c)
+
+target_link_libraries(cxxabi ${libraries})
+
+# Setup flags.
+append_if(compile_flags LIBCXXABI_HAS_FPIC_FLAG -fPIC)
+append_if(link_flags LIBCXXABI_HAS_NODEFAULTLIBS_FLAG -nodefaultlibs)
+
+if ( APPLE )
+  if ( CMAKE_OSX_DEPLOYMENT_TARGET STREQUAL "10.6" )
+    list(APPEND compile_flags "-U__STRICT_ANSI__")
+    list(APPEND link_flags
+      "-compatibility_version 1"
+      "-current_version ${LIBCXXABI_VERSION}"
+      "-install_name /usr/lib/libc++abi.1.dylib"
+      "/usr/lib/libSystem.B.dylib")
+  else()
+    list(APPEND link_flags
+      "-compatibility_version 1"
+      "-install_name /usr/lib/libc++abi.1.dylib")
+  endif()
+endif()
+
+string(REPLACE ";" " " link_flags "${link_flags}")
+
+set_target_properties(cxxabi
+  PROPERTIES
+    COMPILE_FLAGS "${compile_flags}"
+    LINK_FLAGS    "${link_flags}"
+    OUTPUT_NAME   "c++abi"
+    VERSION       "1.0"
+    SOVERSION     "1"
+  )
+
+install(TARGETS cxxabi
+  LIBRARY DESTINATION lib
+  ARCHIVE DESTINATION lib
+  )
diff --git a/www/index.html b/www/index.html
index a174afd..4960b95 100644
--- a/www/index.html
+++ b/www/index.html
@@ -79,6 +79,16 @@
   <li><code>svn co http://llvm.org/svn/llvm-project/libcxxabi/trunk libcxxabi</code></li>
   </ul>
 
+  <p>To build on Linux:</p>
+  <ul>
+  <li><code>cd libcxxabi</code></li>
+  <li><code>mkdir build && cd build</code></li>
+  <li><code>CC=clang CXX=clang++ cmake LIBCXXABI_LIBCXX_PATH=path/to/libcxx ..</code></li>
+  <li><code>make</code></li>
+  </ul>
+
+  <p>You can also checkout libcxxabi to llvm/projects to build in tree.<p>
+
   <p>Send discussions to the
   (<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev">clang mailing list</a>).</p>
 
-- 
2.0.0.526.g5318336




More information about the cfe-commits mailing list