瀏覽代碼

Make various improvements to the CMake scripts

Set minimum required to 3.4 across the board. Use standard
GNUInstallDirs variables instead of custom INSTALL_BIN_DIR and
INSTALL_LIB_DIR. Use generator expressions and commands instead of
variables when possible. Use Runtime, Development, and Libraries
components in the installation. Set up the layout of the build tree
to match the installation tree. Replace compiler-specific flags with
more general cross-platform settings when possible. Update COMPILE
with new configure and install instructions. Add alias targets.
Kyle Edwards 6 年之前
父節點
當前提交
91bb6b28fd
共有 5 個文件被更改,包括 109 次插入77 次删除
  1. 49 51
      CMakeLists.txt
  2. 9 9
      COMPILE
  3. 13 9
      sq/CMakeLists.txt
  4. 19 4
      sqstdlib/CMakeLists.txt
  5. 19 4
      squirrel/CMakeLists.txt

+ 49 - 51
CMakeLists.txt

@@ -1,76 +1,74 @@
-if(MSVC)
-  cmake_minimum_required(VERSION 3.4)
-else()
-  cmake_minimum_required(VERSION 2.8)
-endif()
+cmake_minimum_required(VERSION 3.4)
+project(squirrel VERSION 3.1 LANGUAGES C CXX)
 
 option(DISABLE_STATIC "Avoid building/installing static libraries.")
 option(LONG_OUTPUT_NAMES "Use longer names for binaries and libraries: squirrel3 (not sq).")
 
-set(CMAKE_INSTALL_PREFIX "${CMAKE_SOURCE_DIR}" CACHE PATH "")
 if (NOT CMAKE_BUILD_TYPE)
   set(CMAKE_BUILD_TYPE "Release")
 endif ()
 
-project(squirrel C CXX)
+include(GNUInstallDirs)
 
-include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
+set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}")
+set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}")
+set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}")
+set(CMAKE_CXX_STANDARD 11)
 
 if(CMAKE_COMPILER_IS_GNUCXX)
-  set(SQ_FLAGS -fno-exceptions -fno-strict-aliasing -Wall -Wextra -pedantic -Wcast-qual)
-
-  if(CMAKE_BUILD_TYPE STREQUAL "Release")
-    set(SQ_FLAGS ${SQ_FLAGS} -O3)
-  elseif(CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
-    set(SQ_FLAGS ${SQ_FLAGS} -O3 -g)
-  elseif(CMAKE_BUILD_TYPE STREQUAL "MinSizeRel")
-    set(SQ_FLAGS ${SQ_FLAGS} -Os)
-  elseif(CMAKE_BUILD_TYPE STREQUAL "Debug")
-    set(SQ_FLAGS ${SQ_FLAGS} -pg -pie -gstabs -g3 -Og)
-  endif()
-
-  if(CMAKE_VERSION VERSION_GREATER 3)
-    add_compile_options(${SQ_FLAGS})
-  else()
-    add_definitions(${SQ_FLAGS})
-  endif()
-
-  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti -std=c++0x")
+  add_compile_options(
+    "$<$<COMPILE_LANGUAGE:CXX>:-fno-rtti;-fno-exceptions>"
+    -fno-strict-aliasing
+    -Wall
+    -Wextra
+    -pedantic
+    -Wcast-qual
+    "$<$<CONFIG:Release>:-O3>"
+    "$<$<CONFIG:RelWithDebInfo>:-O3;-g>"
+    "$<$<CONFIG:MinSizeRel>:-Os>"
+    "$<$<CONFIG:Debug>:-pg;-pie;-gstabs;-g3;-Og>"
+    )
 elseif(MSVC)
   set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
   add_definitions(-D_CRT_SECURE_NO_WARNINGS)
 endif()
 
-if(CMAKE_SIZEOF_VOID_P EQUAL 8)
-  add_definitions(-D_SQ64)
-endif()
+add_subdirectory(squirrel)
+add_subdirectory(sqstdlib)
+add_subdirectory(sq)
 
-if(NOT SQ_DISABLE_INSTALLER)
-  if(NOT INSTALL_BIN_DIR)
-    set(INSTALL_BIN_DIR bin)
+if(CMAKE_SIZEOF_VOID_P EQUAL 8)
+  set(tgts)
+  if(NOT DISABLE_DYNAMIC)
+    list(APPEND tgts squirrel sqstdlib sq)
   endif()
-
-  if(NOT INSTALL_LIB_DIR)
-    set(INSTALL_LIB_DIR lib)
+  if(NOT DISABLE_STATIC)
+    list(APPEND tgts squirrel_static sqstdlib_static sq_static)
   endif()
+  foreach(t ${tgts})
+    target_compile_definitions(${t} PUBLIC -D_SQ64)
+  endforeach()
 endif()
 
-add_subdirectory(squirrel)
-add_subdirectory(sqstdlib)
-add_subdirectory(sq)
-
-if(NOT WIN32 AND NOT DISABLE_DYNAMIC)
+if(NOT DISABLE_DYNAMIC)
   set_target_properties(squirrel sqstdlib PROPERTIES SOVERSION 0 VERSION 0.0.0)
 endif()
 
-if(INSTALL_INC_DIR)
-  set(SQ_PUB_HEADERS include/sqconfig.h
-                     include/sqstdaux.h
-                     include/sqstdblob.h
-                     include/sqstdio.h
-                     include/sqstdmath.h
-                     include/sqstdstring.h
-                     include/sqstdsystem.h
-                     include/squirrel.h)
-  install(FILES ${SQ_PUB_HEADERS} DESTINATION ${INSTALL_INC_DIR})
+if(NOT SQ_DISABLE_INSTALLER AND NOT SQ_DISABLE_HEADER_INSTALLER)
+  install(FILES
+    include/sqconfig.h
+    include/squirrel.h
+    DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
+    COMPONENT Development
+    )
+  install(FILES
+    include/sqstdaux.h
+    include/sqstdblob.h
+    include/sqstdio.h
+    include/sqstdmath.h
+    include/sqstdstring.h
+    include/sqstdsystem.h
+    DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
+    COMPONENT Development
+    )
 endif()

+ 9 - 9
COMPILE

@@ -38,23 +38,23 @@ under Linux, type something like
  $ make install
  $ cd ..; rm -r build
 
-The default installation directory will be the top source directory,
-i. e. the binaries will go into bin/ and the libraries into lib/. You
-can change this behavior by calling CMake like this:
+The default installation directory will be /usr/local on Unix platforms,
+and C:/Program Files/squirrel on Windows. The binaries will go into bin/
+and the libraries into lib/. You can change this behavior by calling CMake like
+this:
 
  $ cmake .. -DCMAKE_INSTALL_PREFIX=/some/path/on/your/system
 
-With the INSTALL_BIN_DIR and INSTALL_LIB_DIR options, the directories
+With the CMAKE_INSTALL_BINDIR and CMAKE_INSTALL_LIBDIR options, the directories
 the binaries & libraries will go in (relative to CMAKE_INSTALL_PREFIX)
 can be specified. For instance,
 
- $ cmake .. -DINSTALL_LIB_DIR=lib64
+ $ cmake .. -DCMAKE_INSTALL_LIBDIR=lib64
 
 will install the libraries into a 'lib64' subdirectory in the top
-source directory. If INSTALL_INC_DIR is set, the public header files
-will be installed into the directory the value of INSTALL_INC_DIR
-points to. There is no default directory - if you want only the
-binaries and no headers, just don't specify INSTALL_INC_DIR, and no
+source directory. The public header files will be installed into the directory
+the value of CMAKE_INSTALL_INCLUDEDIR points to. If you want only the
+binaries and no headers, just set -DSQ_DISABLE_HEADER_INSTALLER=ON, and no
 header files will be installed.
 
 Under Windows, it is probably easiest to use the CMake GUI interface,

+ 13 - 9
sq/CMakeLists.txt

@@ -1,22 +1,30 @@
+set(CMAKE_C_STANDARD 99)
 if(NOT DISABLE_DYNAMIC)
-  if(CMAKE_COMPILER_IS_GNUCXX)
-    set_source_files_properties(sq.c PROPERTIES COMPILE_FLAGS -std=c99)
-  endif()
   add_executable(sq sq.c)
+  add_executable(squirrel::interpreter ALIAS sq)
   set_target_properties(sq PROPERTIES LINKER_LANGUAGE C)
   target_link_libraries(sq squirrel sqstdlib)
   if(NOT SQ_DISABLE_INSTALLER)
-    install(TARGETS sq RUNTIME DESTINATION ${INSTALL_BIN_DIR})
+    install(TARGETS sq EXPORT squirrel RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT Runtime)
   endif()
+  target_include_directories(sq PUBLIC
+    "$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>"
+    "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
+    )
 endif()
 
 if(NOT DISABLE_STATIC)
   add_executable(sq_static sq.c)
+  add_executable(squirrel::interpreter_static ALIAS sq)
   set_target_properties(sq_static PROPERTIES LINKER_LANGUAGE C)
   target_link_libraries(sq_static squirrel_static sqstdlib_static)
   if(NOT SQ_DISABLE_INSTALLER)
-    install(TARGETS sq_static RUNTIME DESTINATION ${INSTALL_BIN_DIR})
+    install(TARGETS sq_static EXPORT squirrel RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT Runtime)
   endif()
+  target_include_directories(sq_static PUBLIC
+    "$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>"
+    "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
+    )
 endif()
 
 if(LONG_OUTPUT_NAMES)
@@ -28,7 +36,3 @@ if(LONG_OUTPUT_NAMES)
     set_target_properties(sq_static PROPERTIES OUTPUT_NAME squirrel3_static)
   endif()
 endif()
-
-if(CMAKE_COMPILER_IS_GNUCXX AND NOT DISABLE_STATIC)
-  set_target_properties(sq_static PROPERTIES COMPILE_FLAGS "-static -Wl,-static")
-endif()

+ 19 - 4
sqstdlib/CMakeLists.txt

@@ -9,19 +9,34 @@ set(SQSTDLIB_SRC sqstdaux.cpp
 
 if(NOT DISABLE_DYNAMIC)
   add_library(sqstdlib SHARED ${SQSTDLIB_SRC})
+  add_library(squirrel::sqstdlib ALIAS sqstdlib)
   target_link_libraries(sqstdlib squirrel)
   if(NOT SQ_DISABLE_INSTALLER)
-    install(TARGETS sqstdlib RUNTIME DESTINATION ${INSTALL_BIN_DIR}
-                             LIBRARY DESTINATION ${INSTALL_LIB_DIR}
-                             ARCHIVE DESTINATION ${INSTALL_LIB_DIR})
+    install(TARGETS sqstdlib EXPORT squirrel
+      RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT Libraries
+      LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT Libraries NAMELINK_SKIP
+      ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT Libraries
+      )
+    install(TARGETS sqstdlib EXPORT squirrel
+      LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT Development NAMELINK_ONLY
+      )
   endif()
+  target_include_directories(sqstdlib PUBLIC
+    "$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>"
+    "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
+    )
 endif()
 
 if(NOT DISABLE_STATIC)
   add_library(sqstdlib_static STATIC ${SQSTDLIB_SRC})
+  add_library(squirrel::sqstdlib_static ALIAS sqstdlib_static)
   if(NOT SQ_DISABLE_INSTALLER)
-    install(TARGETS sqstdlib_static ARCHIVE DESTINATION ${INSTALL_LIB_DIR})
+    install(TARGETS sqstdlib_static EXPORT squirrel ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT Development)
   endif()
+  target_include_directories(sqstdlib_static PUBLIC
+    "$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>"
+    "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
+    )
 endif()
 
 if(LONG_OUTPUT_NAMES)

+ 19 - 4
squirrel/CMakeLists.txt

@@ -13,18 +13,33 @@ set(SQUIRREL_SRC sqapi.cpp
 
 if(NOT DISABLE_DYNAMIC)
   add_library(squirrel SHARED ${SQUIRREL_SRC})
+  add_library(squirrel::squirrel ALIAS squirrel)
   if(NOT SQ_DISABLE_INSTALLER)
-    install(TARGETS squirrel RUNTIME DESTINATION ${INSTALL_BIN_DIR}
-                         LIBRARY DESTINATION ${INSTALL_LIB_DIR}
-                         ARCHIVE DESTINATION ${INSTALL_LIB_DIR})
+    install(TARGETS squirrel EXPORT squirrel
+      RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT Libraries
+      LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT Libraries NAMELINK_SKIP
+      ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT Libraries
+      )
+    install(TARGETS squirrel EXPORT squirrel
+      LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT Development NAMELINK_ONLY
+      )
   endif()
+  target_include_directories(squirrel PUBLIC
+    "$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>"
+    "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
+    )
 endif()
 
 if(NOT DISABLE_STATIC)
   add_library(squirrel_static STATIC ${SQUIRREL_SRC})
+  add_library(squirrel::squirrel_static ALIAS squirrel_static)
   if(NOT SQ_DISABLE_INSTALLER)
-    install(TARGETS squirrel_static ARCHIVE DESTINATION ${INSTALL_LIB_DIR})
+    install(TARGETS squirrel_static EXPORT squirrel ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT Development)
   endif()
+  target_include_directories(squirrel_static PUBLIC
+    "$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>"
+    "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
+    )
 endif()
 
 if(LONG_OUTPUT_NAMES)