Skip to content

Commit

Permalink
Merge pull request #4 from Conzxy/win
Browse files Browse the repository at this point in the history
Win
  • Loading branch information
Conzxy authored Apr 5, 2023
2 parents fe46670 + eb5d4e2 commit 082d8fe
Show file tree
Hide file tree
Showing 32 changed files with 753 additions and 514 deletions.
133 changes: 14 additions & 119 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,59 +1,21 @@
cmake_minimum_required(VERSION 3.10)
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)

project(mmkv)
include(cmake/utils.cmake)

if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release")
endif()
mmkv_extract_version()

set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g")
set(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG")
set(CMAKE_CXX_FLAGS_MINSIZEREL "-Os -DNDEBUG")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-Og")
project(mmkv VERSION ${MMKV_VERSION})

message(STATUS "CXX compiler: ${CMAKE_CXX_COMPILER}")
message(STATUS "Build Mmkv v${MMKV_VERSION}")

set(CXX_FLAGS
-Wall
-Wno-attributes
-Wextra
-Wno-return-local-addr
-Wno-unused-parameter
-Wno-unused-function
-Wno-switch
-Wno-format-security
# support INT2DOUBLE
-Wno-strict-aliasing
-Werror
# make non-trivial(but like "trivial") class can reallocate
-Wno-class-memaccess
-Wno-implicit-fallthrough
#-Wconversion
#-Wshadow
-Wno-maybe-uninitialized
-Wwrite-strings # in fact, this is default specified
#cxx standard
-std=c++14
-pthread

# linker opt
-rdynamic
# machine opt
-march=native
)

if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
list(REMOVE_ITEM CXX_FLAGS "-Wno-return-local-addr")
list(REMOVE_ITEM CXX_FLAGS "-rdynamic")
list(APPEND CXX_FLAGS "-Wthread-safety")
endif()

string(REPLACE ";" " " CMAKE_CXX_FLAGS "${CXX_FLAGS}")

message(STATUS "CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}")
message(STATUS "BUILD_TYPE: ${CMAKE_BUILD_TYPE}")
include(cmake/platform.cmake)
include(cmake/build_type.cmake)
include(cmake/compile_flag.cmake)
include(cmake/cache_var.cmake)
include(cmake/install.cmake)

set(CMAKE_CXX_STANDARD 14)

set(MMKV_SOURCE_DIR ${PROJECT_SOURCE_DIR}/mmkv)
set(THIRD_PARTY_DIR ${PROJECT_SOURCE_DIR}/third-party)
Expand All @@ -63,78 +25,11 @@ set(TAKINA_DIR ${THIRD_PARTY_DIR}/takina)
set(CHISATO_DIR ${THIRD_PARTY_DIR}/chisato)
set(TERNARY_DIR ${THIRD_PARTY_DIR}/ternary-tree)
set(HKLUA_DIR ${THIRD_PARTY_DIR}/hklua)

set(LINENOISE_DIR ${THIRD_PARTY_DIR}/linenoise-ng/include)
set(XXHASH_DIR ${THIRD_PARTY_DIR}/xxHash)

include_directories(${KANON_DIR})
include_directories(${PROJECT_SOURCE_DIR})
include_directories(${LINENOISE_DIR})
include_directories(${XXHASH_DIR})
include_directories(${HKLUA_DIR})

add_subdirectory(${KANON_DIR})
link_directories(${XXHASH_DIR})

# I don't want to use the builtin variable ${BUILD_SHARED_LIBS}
# since it you don't set it to ON explicitly, then the NOT
# ${BUILD_SHARED_LIBS} will be false, and generated libraries
# are static. That's not I wanted behavior. I want a variable,
# it can build shared libraries default even though I don't set
# it explitly, and the build of static libraries is an option.
set(BUILD_STATIC_LIBS OFF CACHE BOOL "Build static libraries")

macro (GenLib lib)
#if (NOT ${BUILD_SHARED_LIBS})
message(STATUS "Source list: ${ARGN}")
if (${BUILD_STATIC_LIBS})
message(STATUS "Build static library: ${lib}")
add_library(${lib} STATIC ${ARGN})
set_target_properties(${lib}
PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
)
else ()
message(STATUS "Build shared library: ${lib}")
add_library(${lib} SHARED ${ARGN})
set_target_properties(${lib}
PROPERTIES
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
)
endif (${BUILD_STATIC_LIBS})
endmacro ()

macro (GenApplication app_name src_list lib_list)
set(SRC_LIST ${${src_list}})
set(LIB_LIST ${${lib_list}})
message(STATUS "application: ${app_name}")
message(STATUS "Source list: ${SRC_LIST}")

add_executable(${app_name} EXCLUDE_FROM_ALL ${SRC_LIST})
list (LENGTH LIB_LIST lib_list_len)
if (NOT ${lib_list_len} EQUAL 0)
message(STATUS "Lib list: ${LIB_LIST}")
target_link_libraries(${app_name} ${LIB_LIST})
endif ()

set_target_properties(${app_name}
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/bin"
)
endmacro()

macro (SetLibName var prefix)
if (${CMAKE_BUILD_TYPE} STREQUAL "Release")
set(${var} ${prefix})
else ()
set(${var} "${prefix}_debug")
endif ()
endmacro ()

# message(STATUS "BUILD_SHARED_LIBS = ${BUILD_SHARED_LIBS}")
message(STATUS "BUILD_ALL_TESTS = ${BUILD_ALL_TESTS}")
message(STATUS "BUILD_ALL_EXAMPLES = ${BUILD_ALL_EXAMPLES}")

add_subdirectory(third-party)
add_subdirectory(mmkv)
add_subdirectory(test)

if (MMKV_TESTS)
add_subdirectory(test)
endif ()
15 changes: 15 additions & 0 deletions cmake/build_type.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release")
endif()

message(STATUS "CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")

if (CMAKE_BUILD_TYPE STREQUAL "Release")
message(STATUS "CMAKE_CXX_FLAGS_RELEASE: ${CMAKE_CXX_FLAGS_RELEASE}")
elseif (CMAKE_BUILD_TYPE STREQUAL "Debug")
message(STATUS "CMAKE_CXX_FLAGS_DEBUG")
elseif (CMAKE_BUILD_TYPE STREQUAL "MinSizeRel")
message(STATUS "CMAKE_CXX_FLAGS_MINSIZEREL: ${CMAKE_CXX_FLAGS_MINSIZEREL}")
elseif (CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
message(STATUS "CMAKE_CXX_FLAGS_RELWITHDEBINFO: ${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
endif ()
19 changes: 19 additions & 0 deletions cmake/cache_var.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Check if mmkv is being used directly or via add_subdirectory, but allow overriding
if(NOT DEFINED MMKV_MAIN_PROJECT)
if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
set(MMKV_MAIN_PROJECT ON)
else()
set(MMKV_MAIN_PROJECT OFF)
endif()
endif()

option(MMKV_BUILD_STATIC_LIBS "Build static library of mmkv" ON)
# to install files to proper destination actually.
option(MMKV_INSTALL "Generate the install target" ${MMKV_MAIN_PROJECT})

option(MMKV_TESTS "Generate mmkv test targets" OFF)

# User can determine whether to build all tests when build target all
# e.g. cmake --build */MMKV/build [--target all -j 2]
# If this option is OFF, user should specify target manually.
option(MMKV_BUILD_ALL_TESTS "Build tests when --target all(default) is specified" OFF)
57 changes: 57 additions & 0 deletions cmake/compile_flag.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
set(CMAKE_CXX_FLAGS "/DWIN32 /D_WINDOWS /W0 /GR /EHsc /Zc:__cplusplus")
set(CMAKE_CXX_FLAGS_DEBUG "/MDd /Zi /Ob0 /Od /RTC1 /Zc:__cplusplus")
set(CMAKE_CXX_FLAGS_RELEASE "/MD /O2 /Ob2 /DNDEBUG /Zc:__cplusplus")
set(CMAKE_CXX_FLAGS_MINSIZEREL "/MD /O1 /Ob1 /DNDEBUG /Zc:__cplusplus")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "/MD /Zi /O2 /Ob1 /DNDEBUG /Zc:__cplusplus")
else ()
# FYI https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -g")
set(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG")
set(CMAKE_CXX_FLAGS_MINSIZEREL "-Os -DNDEBUG")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-Og")

# FYI https://gcc.gnu.org/onlinedocs/gcc-5.5.0/gcc/Link-Options.html#Link-Options
# FYI https://gcc.gnu.org/onlinedocs/gcc-5.5.0/gcc/Warning-Options.html#Warning-Options

set(CXX_FLAGS
-Wall
-Wno-attributes
-Wextra
-Wno-return-local-addr
-Wno-unused-parameter
-Wno-unused-function
-Wno-switch
-Wno-format-security
# support INT2DOUBLE
-Wno-strict-aliasing
-Werror
# make non-trivial(but like "trivial") class can reallocate
-Wno-class-memaccess
-Wno-implicit-fallthrough
#-Wconversion
#-Wshadow
-Wno-maybe-uninitialized
-Wwrite-strings # in fact, this is default specified
#cxx standard
-pthread

# linker opt
-rdynamic
# machine opt
-march=native
)

# Clang和GCC有些选项是不通用的
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
list(REMOVE_ITEM CXX_FLAGS "-Wno-return-local-addr")
list(REMOVE_ITEM CXX_FLAGS "-rdynamic")
list(APPEND CXX_FLAGS "-Wthread-safety")
endif()

string(REPLACE ";" " " CMAKE_CXX_FLAGS "${CXX_FLAGS}")
endif ()

set(CMAKE_CXX_STANDARD 11)

message(STATUS "CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}")
51 changes: 51 additions & 0 deletions cmake/install.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
include(GNUInstallDirs)

function (conzxy_install)
cmake_parse_arguments(PARSE_ARGV 0 CONZXY "" "PROJECT;NAMESPACE" "TARGETS")

set(project_config_in_file "${CMAKE_CURRENT_LIST_DIR}/cmake/${CONZXY_PROJECT}Config.cmake.in")
set(project_config_out_file "${CMAKE_CURRENT_BINARY_DIR}/${CONZXY_PROJECT}Config.cmake")
set(config_targets_filename "${CONZXY_PROJECT}ConfigTargets.cmake")
set(config_version_file "${CMAKE_CURRENT_BINARY_DIR}/${CONZXY_PROJECT}ConfigVersion.cmake")
set(export_dest_dir "${CMAKE_INSTALL_LIBDIR}/cmake/${CONZXY_PROJECT}")

# Specify rules of targets
install(
TARGETS ${CONZXY_TARGETS}
EXPORT ${CONZXY_PROJECT}
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
)

# Export the ConfigTargets.cmake to build directory
# To make user use find_package() successfully but don't install,
# you can use export(PACKAGE <name>) command to register build dir to
# ~/.cmake/packages/
export(
EXPORT ${CONZXY_PROJECT}
NAMESPACE ${CONZXY_NAMESPACE}::
FILE "${CMAKE_CURRENT_BINARY_DIR}/${config_targets_filename}")

# Generate and Install ConfigTargets.cmake
install(EXPORT ${CONZXY_PROJECT}
DESTINATION "${export_dest_dir}"
NAMESPACE ${CONZXY_NAMESPACE}::
FILE ${config_targets_filename})

include(CMakePackageConfigHelpers)
# Generate Config.cmake and ConfigVersion.cmake
# (<in> <out>)
configure_package_config_file("${project_config_in_file}" "${project_config_out_file}"
INSTALL_DESTINATION "${export_dest_dir}")
write_basic_package_version_file("${config_version_file}" COMPATIBILITY SameMajorVersion)
message(STATUS "Config file path = ${project_config_out_file}")
message(STATUS "ConfigVersion file path = ${config_version_file}")
# Install Config.cmake and ConfigVersion.cmake
install(FILES "${project_config_out_file}" "${config_version_file}" DESTINATION "${export_dest_dir}")
endfunction ()

message(STATUS "CMAKE_INSTALL_INCLUDEDIR: ${CMAKE_INSTALL_INCLUDEDIR}")
message(STATUS "CMAKE_INSTALL_BINDIR: ${CMAKE_INSTALL_BINDIR}")
message(STATUS "CMAKE_INSTALL_LIBDIR: ${CMAKE_INSTALL_LIBDIR}")
message(STATUS "CMAKE_INSTALL_PREFIX: ${CMAKE_INSTALL_PREFIX}")
13 changes: 13 additions & 0 deletions cmake/platform.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# In other platform(eg. windows), generating base library only.
if (UNIX AND NOT APPLE)
message(STATUS "Mmkv on unix/linux/solaris/... platform")
set(MMKV_ON_UNIX ON)
else ()
set(MMKV_ON_UNIX OFF)
endif ()

if (WIN32)
message(STATUS "Mmkv on windows")
set(MMKV_ON_WIN ON)
endif ()

Loading

0 comments on commit 082d8fe

Please sign in to comment.