Skip to content

Commit

Permalink
Add Windows and CMake support
Browse files Browse the repository at this point in the history
Fix build issue on macOS for `phtread_threadid_np`
  • Loading branch information
tuduongquyet authored and deemar committed Feb 10, 2023
1 parent 0c2b0b1 commit b8d9634
Show file tree
Hide file tree
Showing 35 changed files with 1,249 additions and 57 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,8 @@ doc/*.htoc
doc/*.tex
test/press*
test/*.png
cscope*
release.h
*.pyc

*.dylib
78 changes: 78 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# create by lsm <[email protected]>
# cmake

cmake_minimum_required(VERSION 2.8.5)

message(STATUS "path : ${CMAKE_FIND_ROOT_PATH}")
project(zlog)
message(STATUS "path : ${CMAKE_FIND_ROOT_PATH}")

set(CMAKE_MODULE_PATH ${zlog_SOURCE_DIR}/cmake)

#=====================================
# version of zlog
#=====================================
SET(CPACK_PACKAGE_VERSION_MAJOR "1")
SET(CPACK_PACKAGE_VERSION_MINOR "2")
SET(CPACK_PACKAGE_VERSION_PATCH "12")
SET(CPACK_RPM_PACKAGE_RELEASE 1) #release version.
SET(CPACK_PACKAGE_VERSION "${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}")
SET(zlog_ver ${CPACK_PACKAGE_VERSION})
SET(zlog_so_ver ${CPACK_PACKAGE_VERSION_MAJOR})

#=======================================================

message(STATUS "plateform : ${CMAKE_SYSTEM}")

add_definitions("-g -Wall -Wstrict-prototypes")
set(CMAKE_C_FLAGS "-std=c99 -pedantic -D_DEFAULT_SOURCE")
set(CMAKE_C_FLAGS_DEBUG "-ggdb3 -DDEBUG")
set(CMAKE_C_FLAGS_RELEASE "-O2")

if (WIN32)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DWINVER=0x0500 -D_WIN32_WINNT=0x0500 ")
endif()

#=====================================================
# include dir
# include_directories(include)
#=====================================================

#=====================================================
# lib output path.
cmake_policy(SET CMP0015 NEW)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${zlog_BINARY_DIR}/lib")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${zlog_BINARY_DIR}/lib")
# bin output path.
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${zlog_BINARY_DIR}/bin")
#=====================================================

#=====================================================
# link path.
link_directories(${CMAKE_LIBRARY_OUTPUT_DIRECTORY})
#=====================================================

#=====================================================
# library depend.
set(Need_THREAD 1)
if (WIN32)
set(Need_UNIXEM 1)
endif()

include(cmake/LoadLibraries.cmake)
#=====================================================

#========================================================
# sub dir
add_subdirectory(src)
add_subdirectory(cpack)
#========================================================

#========================================================
# for unittest, call "cmake .. -DUNIT_TEST=on"
if(UNIT_TEST)
enable_testing()
add_subdirectory(test)
endif()
#========================================================

27 changes: 27 additions & 0 deletions cmake/FindUnixem.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# FindUnixem.cmake
# author lsm <[email protected]>

if (NOT UNIXEM_FOUND)
if (NOT UNIXEM_INCLUDE_DIR)
find_path(UNIXEM_INCLUDE_DIR
NAMES
unixem/unixem.h
ONLY_CMAKE_FIND_ROOT_PATH
)
endif()

if (NOT UNIXEM_LIBRARY)
find_library(UNIXEM_LIBRARY
NAMES unixem
ONLY_CMAKE_FIND_ROOT_PATH
)
endif (NOT UNIXEM_LIBRARY)

message(STATUS " UNIXEM_INCLUDE_DIR = ${UNIXEM_INCLUDE_DIR}")
message(STATUS " UNIXEM_LIBRARY = ${UNIXEM_LIBRARY}")

if (UNIXEM_INCLUDE_DIR AND UNIXEM_LIBRARY)
set(UNIXEM_FOUND TRUE)
endif (UNIXEM_INCLUDE_DIR AND UNIXEM_LIBRARY)
endif()

19 changes: 19 additions & 0 deletions cmake/LoadLibraries.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#=======================================================
# 支持多线程
# 对于需要多线程的库,使用以下命令包含连接库:
# target_link_libraries(xxx ${CMAKE_THREAD_PREFER_PTHREAD})
#=======================================================
if(Need_THREAD)
find_package(Threads REQUIRED)
if(NOT CMAKE_THREAD_PREFER_PTHREAD)
set(CMAKE_THREAD_PREFER_PTHREAD ${CMAKE_THREAD_LIBS_INIT})
endif()
message(STATUS "thread lib : ${CMAKE_THREAD_PREFER_PTHREAD}")
endif(Need_THREAD)

if(Need_UNIXEM)
find_package(Unixem)
if (NOT UNIXEM_FOUND)
message(FATAL_ERROR "unixem lib not found!")
endif()
endif()
40 changes: 40 additions & 0 deletions cmake/toolchain-mingw64.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

# this one is important
SET(CMAKE_SYSTEM_NAME Windows)

# specify the cross compiler
find_path(MINGW_BIN_PATH gcc.exe PATHS c:/mingw64 d:/mingw64 c:/mingw-build/mingw64 d:/mingw-build/mingw64 PATH_SUFFIXES bin)

if (MINGW_PATH_NOTFOUND)
message(FATAL "mingw64 not found!")
endif()

get_filename_component(MINGW_PATH ${MINGW_BIN_PATH} PATH)

SET(CMAKE_GENERATOR "MinGW Makefiles")

SET(CMAKE_C_COMPILER gcc)
SET(CMAKE_CXX_COMPILER g++)

SET(CMAKE_RC_COMPILER windres)
set(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> <FLAGS> -O coff <DEFINES> -i <SOURCE> -o <OBJECT>")

#SET(_CMAKE_TOOLCHAIN_PREFIX x86_64-w64-mingw32-)
SET(_CMAKE_TOOLCHAIN_LOCATION ${MINGW_BIN_PATH})

# where is the target environment
SET(CMAKE_FIND_ROOT_PATH ${MINGW_PATH} ${MINGW_PATH}/x86_64-w64-mingw32)

SET(CMAKE_SYSTEM_INCLUDE_PATH "${CMAKE_SYSTEM_INCLUDE_PATH} ${MINGW_PATH}/include ${MINGW_PATH}/x86_64-w64-mingw32/include")
SET(CMAKE_SYSTEM_LIBRARY_PATH "${CMAKE_SYSTEM_LIBRARY_PATH} ${MINGW_PATH}/lib ${MINGW_PATH}/x86_64-w64-mingw32/lib")
SET(CMAKE_SYSTEM_PROGRAM_PATH "${CMAKE_SYSTEM_PROGRAM_PATH} ${MINGW_PATH}/bin ${MINGW_PATH}/x86_64-w64-mingw32/bin")

# printf support %sz format.
add_definitions("-D_POSIX")

# search for programs in the build host directories
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# for libraries and headers in the target directories
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

56 changes: 56 additions & 0 deletions cpack/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# create by lsm <[email protected]>

#======================================
# vendor info
#======================================
SET(CPACK_PACKAGE_VENDOR "zlog")
SET(CPACK_PACKAGE_CONTACT "[email protected]")
SET(CPACK_RPM_PACKAGE_LICENSE "LGPL")

#======================================
# default install prefix.
#======================================
SET(CPACK_PACKAGING_INSTALL_PREFIX "/usr/local")
SET(CPACK_RPM_PACKAGE_GROUP "System Environment/Base")

#=================================
# set platform.
#=================================
message(STATUS "system process is ${CMAKE_HOST_SYSTEM_PROCESSOR}")
message(STATUS "system process is ${CMAKE_SYSTEM_PROCESSOR}")

IF (NOT CMAKE_SYSTEM_PROCESSOR)
set(CMAKE_SYSTEM_PROCESSOR ${CMAKE_HOST_SYSTEM_PROCESSOR})
endif()

IF(${CMAKE_SYSTEM_PROCESSOR} MATCHES "x86_64|amd64")
set(CMAKE_SYSTEM_PROCESSOR x86_64)
SET(CPACK_DEBIAN_PACKAGE_ARCHITECTURE amd64)
SET(CPACK_RPM_PACKAGE_ARCHITECTURE "x86_64")
ELSEIF(${CMAKE_SYSTEM_PROCESSOR} MATCHES "powerpc64|ppc64")
SET(CPACK_DEBIAN_PACKAGE_ARCHITECTURE powerpc64)
SET(CPACK_RPM_PACKAGE_ARCHITECTURE "ppc64")
ELSEIF(${CMAKE_SYSTEM_PROCESSOR} MATCHES "powerpc|ppc")
SET(CPACK_DEBIAN_PACKAGE_ARCHITECTURE powerpc)
SET(CPACK_RPM_PACKAGE_ARCHITECTURE "ppc")
ELSE()
SET(CPACK_DEBIAN_PACKAGE_ARCHITECTURE i386)
SET(CPACK_RPM_PACKAGE_ARCHITECTURE i386)
ENDIF()
SET(CPACK_SYSTEM_NAME "${CMAKE_SYSTEM_NAME}-${CMAKE_SYSTEM_PROCESSOR}")
SET(CPACK_TOPLEVEL_TAG "${CMAKE_SYSTEM_NAME}-${CMAKE_SYSTEM_PROCESSOR}")

#======================================
# generator setting.
#======================================
SET(CPACK_CMAKE_GENERATOR "Unix Makefiles")
if (WIN32)
SET(CPACK_GENERATOR "NSIS")
else()
SET(CPACK_GENERATOR "RPM;DEB")
endif()

#======================================
# package.
#======================================
add_subdirectory(zlog)
66 changes: 66 additions & 0 deletions cpack/CPackConfig.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#===================================================#
# pack config.
#===================================================#
SET(CPACK_CMAKE_GENERATOR "${CPACK_CMAKE_GENERATOR}")
SET(CPACK_GENERATOR "${CPACK_GENERATOR}")
SET(CPACK_OUTPUT_CONFIG_FILE "${CPACK_OUTPUT_CONFIG_FILE}")
SET(CPACK_INSTALL_CMAKE_PROJECTS "${CPACK_INSTALL_CMAKE_PROJECTS}")

SET(CPACK_PACKAGE_VERSION_MAJOR "${CPACK_PACKAGE_VERSION_MAJOR}")
SET(CPACK_PACKAGE_VERSION_MINOR "${CPACK_PACKAGE_VERSION_MINOR}")
SET(CPACK_PACKAGE_VERSION_PATCH "${CPACK_PACKAGE_VERSION_PATCH}")
SET(CPACK_RPM_PACKAGE_RELEASE "${CPACK_RPM_PACKAGE_RELEASE}") #release version.
SET(CPACK_PACKAGE_VERSION "${CPACK_PACKAGE_VERSION}")

SET(CPACK_PACKAGE_NAME "${CPACK_PACKAGE_NAME}" )
SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "${CPACK_PACKAGE_DESCRIPTION_SUMMARY}" )
SET(CPACK_PACKAGE_DESCRIPTION_FILE "${CPACK_PACKAGE_DESCRIPTION_FILE}" )

SET(CPACK_PACKAGE_VENDOR "${CPACK_PACKAGE_VENDOR}" )
SET(CPACK_PACKAGING_INSTALL_PREFIX "${CPACK_PACKAGING_INSTALL_PREFIX}" )
SET(CPACK_PACKAGE_CONTACT "${CPACK_PACKAGE_CONTACT}" )

SET(CPACK_PACKAGE_INSTALL_REGISTRY_KEY "${CPACK_PACKAGE_NAME} ${CPACK_PACKAGE_VERSION}")

SET(CPACK_SYSTEM_NAME "${CPACK_SYSTEM_NAME}")
SET(CPACK_TOPLEVEL_TAG "${CPACK_TOPLEVEL_TAG}")
SET(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CPACK_PACKAGE_VERSION}-${CPACK_RPM_PACKAGE_RELEASE}-${CPACK_TOPLEVEL_TAG}")

#SET(CPACK_PACKAGE_EXECUTABLES "ccmake;CMake")
#SET(CPACK_STRIP_FILES "bin/ccmake;bin/cmake;bin/cpack;bin/ctest")

set (CPACK_NSIS_MODIFY_PATH, ON)

#=================================
# set platform.
#=================================
SET(CPACK_DEBIAN_PACKAGE_ARCHITECTURE "${CPACK_DEBIAN_PACKAGE_ARCHITECTURE}" )
SET(CPACK_RPM_PACKAGE_ARCHITECTURE "${CPACK_RPM_PACKAGE_ARCHITECTURE}" )

#=================================
# set dependency.
#=================================
SET(CPACK_RPM_PACKAGE_REQUIRES "${CPACK_RPM_PACKAGE_REQUIRES}" )
SET(CPACK_DEBIAN_PACKAGE_DEPENDS "${CPACK_DEBIAN_PACKAGE_DEPENDS}" )

#=================================
# set control script.
#=================================
SET(CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA
"${CMAKE_CURRENT_BINARY_DIR}/preinst;${CMAKE_CURRENT_BINARY_DIR}/postinst;${CMAKE_CURRENT_BINARY_DIR}/prerm;${CMAKE_CURRENT_BINARY_DIR}/postrm")

SET(CPACK_RPM_PRE_INSTALL_SCRIPT_FILE "${CMAKE_CURRENT_BINARY_DIR}/preinst")
SET(CPACK_RPM_POST_INSTALL_SCRIPT_FILE "${CMAKE_CURRENT_BINARY_DIR}/postinst")
SET(CPACK_RPM_PRE_UNINSTALL_SCRIPT_FILE "${CMAKE_CURRENT_BINARY_DIR}/prerm")
SET(CPACK_RPM_POST_UNINSTALL_SCRIPT_FILE "${CMAKE_CURRENT_BINARY_DIR}/postrm")

#===================================================#
# set license
#===================================================#
SET(CPACK_RPM_PACKAGE_LICENSE "${CPACK_RPM_PACKAGE_LICENSE}")

#===================================================#
# others.
#===================================================#
SET(CPACK_RPM_PACKAGE_GROUP "${CPACK_RPM_PACKAGE_GROUP}")

30 changes: 30 additions & 0 deletions cpack/zlog/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# create by lsm <[email protected]>

#===============================
# package info setting
#===============================
SET(CPACK_PACKAGE_NAME "zlog")
SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "log component for Linux/Unix/AIX")
SET(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_HOME_DIRECTORY}/README")
SET(CPACK_INSTALL_CMAKE_PROJECTS "${zlog_BINARY_DIR};zlog;zlog;/")

#=================================
# dependency setting
#=================================
#SET(CPACK_RPM_PACKAGE_REQUIRES "")
#SET(CPACK_DEBIAN_PACKAGE_DEPENDS "")

#===============================
# copy file to build directory.
#===============================
SET(CPACK_OUTPUT_CONFIG_FILE "${CMAKE_CURRENT_BINARY_DIR}/CPackConfig.cmake")
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/../CPackConfig.cmake CPackConfig.cmake)

file(COPY
.

DESTINATION ${CMAKE_CURRENT_BINARY_DIR}

PATTERN CMakeLists.txt EXCLUDE
)

4 changes: 4 additions & 0 deletions cpack/zlog/postinst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

ldconfig

5 changes: 5 additions & 0 deletions cpack/zlog/postrm
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

ldconfig


1 change: 1 addition & 0 deletions cpack/zlog/preinst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#!/usr/bin/env bash
2 changes: 2 additions & 0 deletions cpack/zlog/prerm
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash

Loading

0 comments on commit b8d9634

Please sign in to comment.