-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMakeLists.txt
111 lines (94 loc) · 4.36 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# pyPiCode
# Python C extension module to wrap the PiCode library
#
# See: https://github.com/latchdevel/pyPiCode
#
# Copyright (c) 2022 Jorge Rivera. All right reserved.
# License GNU Lesser General Public License v3.0.
cmake_minimum_required(VERSION 3.18)
project(picode_wrap LANGUAGES C)
if(NOT BUILD_COMPILER)
# Set complier identification
SET(BUILD_COMPILER "${CMAKE_C_COMPILER_ID} ${CMAKE_C_COMPILER_VERSION}" )
MESSAGE( STATUS "C Compiler: " ${BUILD_COMPILER} )
endif()
# Check if set CMAKE_BUILD_TYPE var
if(NOT CMAKE_BUILD_TYPE)
# Set default build type to "release" set -O3 -DNDEBUG
set(DEFAULT_BUILD_TYPE "RELEASE")
SET(CMAKE_BUILD_TYPE ${DEFAULT_BUILD_TYPE})
MESSAGE( STATUS "Build type set to default: " ${CMAKE_BUILD_TYPE} )
else()
# Check if set and valid CMAKE_BUILD_TYPE var
STRING( TOUPPER "${CMAKE_BUILD_TYPE}" CMAKE_BUILD_TYPE )
if((CMAKE_BUILD_TYPE STREQUAL "DEBUG") OR (CMAKE_BUILD_TYPE STREQUAL "RELEASE"))
# If no has parent directory show message
if(NOT hasParent)
MESSAGE( STATUS "Build type set to: " ${CMAKE_BUILD_TYPE} )
endif()
else()
MESSAGE( FATAL_ERROR "If set CMAKE_BUILD_TYPE it must be 'release' or 'debug'")
endif()
endif()
# Setting build type to "debug" add only -g
if(CMAKE_BUILD_TYPE STREQUAL "DEBUG")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DDEBUG")
endif()
# Set C compiler flags
if(CMAKE_COMPILER_IS_GNUC OR CMAKE_C_COMPILER_ID MATCHES "Clang")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-deprecated-declarations")
elseif(MSVC)
set(MSVC_DISABLED_WARNINGS_LIST
"C4201" # warning C4201: nonstandard extension used: nameless struct/union
"C4100" # warning C4100: unreferenced formal parameter
"C4115" # warning C4115: named type definition in parentheses
"C4127" # warning C4127: conditional expression is constant
)
string(REPLACE "C" " -wd" MSVC_DISABLED_WARNINGS_STR ${MSVC_DISABLED_WARNINGS_LIST})
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -W4 ${MSVC_DISABLED_WARNINGS_STR}")
endif()
# Add PiCode source code for libcpicode static library
message(STATUS "\nAdding PiCode library:")
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/libs/PiCode/CMakeLists.txt )
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/.revision.out)
file (STRINGS ${CMAKE_CURRENT_SOURCE_DIR}/.revision.out BUILD_VERSION)
message(STATUS "Setting build version: " ${BUILD_VERSION})
endif()
add_subdirectory( libs/PiCode EXCLUDE_FROM_ALL )
else()
message (FATAL_ERROR "Failed to add PiCode library. Try: 'git submodule update --init'")
endif()
# Checking for Python3
set(Python_REQUESTED_VERSION 3)
message(STATUS "\nFind Python v${Python_REQUESTED_VERSION}:")
find_package(Python ${Python_REQUESTED_VERSION} REQUIRED COMPONENTS Interpreter Development)
if (NOT Python_FOUND)
message (FATAL_ERROR "Failed to find Python ${Python_REQUESTED_VERSION}")
endif()
if (NOT Python_Development.Module_FOUND) # versionadded:: 3.18
message (FATAL_ERROR "Failed to find Python ${Python_REQUESTED_VERSION}, COMPONENT 'Development.Module'")
endif()
# Show config
message (STATUS "Python executable: " ${Python_EXECUTABLE} )
message (STATUS "Python_LIBRARIES: " ${Python_LIBRARIES} )
message (STATUS "Python_INCLUDE_DIRS: " ${Python_INCLUDE_DIRS} )
message (STATUS "Python SOABI: " ${Python_SOABI} )
message (STATUS "SHARED_MODULE_SUFFIX: " ${CMAKE_SHARED_MODULE_SUFFIX} )
# Add main target as Python shared module
# Generated by "swig -python -o picode_wrap.c -outdir pypicode picode_wrap.i"
Python_add_library( ${PROJECT_NAME} MODULE picode_wrap.c)
# Include Python header path to file "Python.h"
include_directories( ${Python_INCLUDE_DIRS} )
# Shared libraries need flag -fPIC
set_property( TARGET ${PROJECT_NAME} PROPERTY POSITION_INDEPENDENT_CODE TRUE )
# If macOS system, builds a Mach-O universal binary with 2 architectures: x86_64 and arm64 for Apple M processors
if (APPLE)
set_property( TARGET ${PROJECT_NAME} PROPERTY COMPILE_FLAGS "-arch arm64 -arch x86_64" )
set_property( TARGET ${PROJECT_NAME} PROPERTY LINK_FLAGS "-arch arm64 -arch x86_64" )
endif()
# Add cPiCode library static link
target_link_libraries( ${PROJECT_NAME} PUBLIC cpicode )
# Disable CMake auto add "lib" prefix when using add_library()
set_property( TARGET ${PROJECT_NAME} PROPERTY PREFIX "_" )
# Set target libary name based on PYTHON_EXTENSION_MODULE_SUFFIX
set_property( TARGET ${PROJECT_NAME} PROPERTY OUTPUT_NAME ${PROJECT_NAME}.${Python_SOABI} )