Skip to content

Commit f573ecb

Browse files
committed
feat: add cmake
1 parent 3ed7b9f commit f573ecb

File tree

7 files changed

+540
-35
lines changed

7 files changed

+540
-35
lines changed

project/raspberrypi4b/CMakeLists.txt

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
#
2+
# Copyright (c) 2015 - present LibDriver All rights reserved
3+
#
4+
# The MIT License (MIT)
5+
#
6+
# Permission is hereby granted, free of charge, to any person obtaining a copy
7+
# of this software and associated documentation files (the "Software"), to deal
8+
# in the Software without restriction, including without limitation the rights
9+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
# copies of the Software, and to permit persons to whom the Software is
11+
# furnished to do so, subject to the following conditions:
12+
#
13+
# The above copyright notice and this permission notice shall be included in all
14+
# copies or substantial portions of the Software.
15+
#
16+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
# SOFTWARE.
23+
#
24+
25+
# set the cmake minimum version
26+
cmake_minimum_required(VERSION 3.0)
27+
28+
# set the project name and language
29+
project(at24cxx C)
30+
31+
# read the version from files
32+
file(READ ${CMAKE_CURRENT_SOURCE_DIR}/cmake/VERSION ${CMAKE_PROJECT_NAME}_VERSION)
33+
34+
# set the project version
35+
set(PROJECT_VERSION ${${CMAKE_PROJECT_NAME}_VERSION})
36+
37+
# set c standard c99
38+
set(CMAKE_C_STANDARD 99)
39+
40+
# enable c standard required
41+
set(CMAKE_C_STANDARD_REQUIRED True)
42+
43+
# set release level
44+
set(CMAKE_BUILD_TYPE Release)
45+
46+
# set the release flags of c
47+
set(CMAKE_C_FLAGS_RELEASE "-O3 -DNDEBUG")
48+
49+
# include cmake package config helpers
50+
include(CMakePackageConfigHelpers)
51+
52+
# find the pkgconfig and use this tool to find the third party packages
53+
find_package(PkgConfig REQUIRED)
54+
55+
# find the third party packages with pkgconfig
56+
pkg_search_module(GPIOD REQUIRED libgpiod)
57+
58+
# include all library header directories
59+
set(LIB_INC_DIRS
60+
${GPIOD_INCLUDE_DIRS}
61+
)
62+
63+
# include all linked libraries
64+
set(LIBS
65+
${GPIOD_LIBRARIES}
66+
)
67+
68+
# include all header directories
69+
set(INC_DIRS
70+
${LIB_INC_DIRS}
71+
${CMAKE_CURRENT_SOURCE_DIR}/../../src
72+
${CMAKE_CURRENT_SOURCE_DIR}/../../interface
73+
${CMAKE_CURRENT_SOURCE_DIR}/../../example
74+
${CMAKE_CURRENT_SOURCE_DIR}/../../test
75+
${CMAKE_CURRENT_SOURCE_DIR}/interface/inc
76+
)
77+
78+
# include all installed headers
79+
file(GLOB INSTL_INCS
80+
${CMAKE_CURRENT_SOURCE_DIR}/../../src/*.h
81+
)
82+
83+
# include all sources files
84+
file(GLOB SRCS
85+
${CMAKE_CURRENT_SOURCE_DIR}/../../src/*.c
86+
)
87+
88+
# include executable source
89+
file(GLOB MAIN
90+
${SRCS}
91+
${CMAKE_CURRENT_SOURCE_DIR}/../../example/*.c
92+
${CMAKE_CURRENT_SOURCE_DIR}/../../test/*.c
93+
${CMAKE_CURRENT_SOURCE_DIR}/interface/src/*.c
94+
${CMAKE_CURRENT_SOURCE_DIR}/driver/src/*.c
95+
${CMAKE_CURRENT_SOURCE_DIR}/src/main.c
96+
)
97+
98+
# enable output as a static library
99+
add_library(${CMAKE_PROJECT_NAME}_static STATIC ${SRCS})
100+
101+
# set the static library include directories
102+
target_include_directories(${CMAKE_PROJECT_NAME}_static PRIVATE ${INC_DIRS})
103+
104+
# set the static library link libraries
105+
target_link_libraries(${CMAKE_PROJECT_NAME}_static
106+
m
107+
)
108+
109+
# rename as ${CMAKE_PROJECT_NAME}
110+
set_target_properties(${CMAKE_PROJECT_NAME}_static PROPERTIES OUTPUT_NAME ${CMAKE_PROJECT_NAME})
111+
112+
# don't delete ${CMAKE_PROJECT_NAME} libs
113+
set_target_properties(${CMAKE_PROJECT_NAME}_static PROPERTIES CLEAN_DIRECT_OUTPUT 1)
114+
115+
# set the static library version
116+
set_target_properties(${CMAKE_PROJECT_NAME}_static PROPERTIES VERSION ${${CMAKE_PROJECT_NAME}_VERSION})
117+
118+
# enable output as a dynamic library
119+
add_library(${CMAKE_PROJECT_NAME} SHARED ${SRCS})
120+
121+
# set the executable program include directories
122+
target_include_directories(${CMAKE_PROJECT_NAME}
123+
PUBLIC $<INSTALL_INTERFACE:include/${CMAKE_PROJECT_NAME}>
124+
PRIVATE ${INC_DIRS}
125+
)
126+
127+
# set the dynamic library link libraries
128+
target_link_libraries(${CMAKE_PROJECT_NAME}
129+
m
130+
)
131+
132+
# rename as ${CMAKE_PROJECT_NAME}
133+
set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES OUTPUT_NAME ${CMAKE_PROJECT_NAME})
134+
135+
# don't delete ${CMAKE_PROJECT_NAME} libs
136+
set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES CLEAN_DIRECT_OUTPUT 1)
137+
138+
# include the public header
139+
set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES PUBLIC_HEADER "${INSTL_INCS}")
140+
141+
# set the dynamic library version
142+
set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES VERSION ${${CMAKE_PROJECT_NAME}_VERSION})
143+
144+
# enable the executable program
145+
add_executable(${CMAKE_PROJECT_NAME}_exe ${MAIN})
146+
147+
# set the executable program include directories
148+
target_include_directories(${CMAKE_PROJECT_NAME}_exe PRIVATE ${INC_DIRS})
149+
150+
# set the executable program link libraries
151+
target_link_libraries(${CMAKE_PROJECT_NAME}_exe
152+
${LIBS}
153+
m
154+
pthread
155+
)
156+
157+
# rename as ${CMAKE_PROJECT_NAME}
158+
set_target_properties(${CMAKE_PROJECT_NAME}_exe PROPERTIES OUTPUT_NAME ${CMAKE_PROJECT_NAME})
159+
160+
# don't delete ${CMAKE_PROJECT_NAME} exe
161+
set_target_properties(${CMAKE_PROJECT_NAME}_exe PROPERTIES CLEAN_DIRECT_OUTPUT 1)
162+
163+
# install the binary
164+
install(TARGETS ${CMAKE_PROJECT_NAME}_exe
165+
RUNTIME DESTINATION bin
166+
)
167+
168+
# install the static library
169+
install(TARGETS ${CMAKE_PROJECT_NAME}_static
170+
ARCHIVE DESTINATION lib
171+
)
172+
173+
# install the dynamic library
174+
install(TARGETS ${CMAKE_PROJECT_NAME}
175+
EXPORT ${CMAKE_PROJECT_NAME}-targets
176+
LIBRARY DESTINATION lib
177+
PUBLIC_HEADER DESTINATION include/${CMAKE_PROJECT_NAME}
178+
)
179+
180+
# make the cmake config file
181+
configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/config.cmake.in
182+
${CMAKE_CURRENT_BINARY_DIR}/cmake/${CMAKE_PROJECT_NAME}-config.cmake
183+
INSTALL_DESTINATION cmake
184+
)
185+
186+
# write the cmake config version
187+
write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/cmake/${CMAKE_PROJECT_NAME}-config-version.cmake
188+
VERSION ${PACKAGE_VERSION}
189+
COMPATIBILITY AnyNewerVersion
190+
)
191+
192+
# install the cmake files
193+
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/cmake/${CMAKE_PROJECT_NAME}-config.cmake"
194+
"${CMAKE_CURRENT_BINARY_DIR}/cmake/${CMAKE_PROJECT_NAME}-config-version.cmake"
195+
DESTINATION cmake
196+
)
197+
198+
# set the export items
199+
install(EXPORT ${CMAKE_PROJECT_NAME}-targets
200+
DESTINATION cmake
201+
)
202+
203+
# add uninstall command
204+
add_custom_target(uninstall
205+
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/uninstall.cmake
206+
)
207+
208+
#include ctest module
209+
include(CTest)
210+
211+
# creat a test
212+
add_test(NAME ${CMAKE_PROJECT_NAME}_test COMMAND ${CMAKE_PROJECT_NAME}_exe -p)

project/raspberrypi4b/Makefile

Lines changed: 148 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,149 @@
1-
CC := gcc
2-
SRC := $(wildcard ./interface/src/*.c) \
3-
$(wildcard ./driver/src/*.c) \
4-
$(wildcard ./src/*.c) \
5-
$(wildcard ../../src/*.c) \
6-
$(wildcard ../../test/*.c) \
7-
$(wildcard ../../example/*.c)
1+
#
2+
# Copyright (c) 2015 - present LibDriver All rights reserved
3+
#
4+
# The MIT License (MIT)
5+
#
6+
# Permission is hereby granted, free of charge, to any person obtaining a copy
7+
# of this software and associated documentation files (the "Software"), to deal
8+
# in the Software without restriction, including without limitation the rights
9+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
# copies of the Software, and to permit persons to whom the Software is
11+
# furnished to do so, subject to the following conditions:
12+
#
13+
# The above copyright notice and this permission notice shall be included in all
14+
# copies or substantial portions of the Software.
15+
#
16+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
# SOFTWARE.
23+
#
24+
25+
# set the project version
26+
VERSION := 1.0.0
27+
28+
# set the application name
29+
APP_NAME := at24cxx
30+
31+
# set the shared libraries name
32+
SHARED_LIB_NAME := libat24cxx.so
33+
34+
# set the static libraries name
35+
STATIC_LIB_NAME := libat24cxx.a
36+
37+
# set the install directories
38+
INSTL_DIRS := /usr/local
39+
40+
# set the include directories
41+
INC_INSTL_DIRS := $(INSTL_DIRS)/include/$(APP_NAME)
42+
43+
# set the library directories
44+
LIB_INSTL_DIRS := $(INSTL_DIRS)/lib
45+
46+
# set the bin directories
47+
BIN_INSTL_DIRS := $(INSTL_DIRS)/bin
48+
49+
# set the compiler
50+
CC := gcc
51+
52+
# set the ar tool
53+
AR := ar
54+
55+
# set the packages name
56+
PKGS := libgpiod
57+
58+
# set the pck-config header directories
59+
LIB_INC_DIRS := $(shell pkg-config --cflags $(PKGS))
60+
61+
# set the linked libraries
62+
LIBS := -lm \
63+
-lpthread
64+
65+
# add the linked libraries
66+
LIBS += $(shell pkg-config --libs $(PKGS))
67+
68+
# set all header directories
69+
INC_DIRS := -I ../../src/ \
70+
-I ../../interface/ \
71+
-I ../../example/ \
72+
-I ../../test/ \
73+
-I ./interface/inc/
74+
75+
# add the linked libraries header directories
76+
INC_DIRS += $(LIB_INC_DIRS)
77+
78+
# set the installing headers
79+
INSTL_INCS := $(wildcard ../../src/*.h)
80+
81+
# set all sources files
82+
SRCS := $(wildcard ../../src/*.c)
83+
84+
# set the main source
85+
MAIN := $(SRCS) \
86+
$(wildcard ../../example/*.c) \
87+
$(wildcard ../../test/*.c) \
88+
$(wildcard ./interface/src/*.c) \
89+
$(wildcard ./driver/src/*.c) \
90+
$(wildcard ./src/main.c)
91+
92+
# set flags of the compiler
893
CFLAGS := -O3 \
9-
-I ./interface/inc/ \
10-
-I ../../interface/ \
11-
-I ../../src/ \
12-
-I ../../test/ \
13-
-I ../../example/
14-
at24cxx : $(SRC)
15-
"$(CC)" $(CFLAGS) $^ -o $@
94+
-DNDEBUG
95+
96+
# set all .PHONY
97+
.PHONY: all
98+
99+
# set the output list
100+
all: $(APP_NAME) $(SHARED_LIB_NAME).$(VERSION) $(STATIC_LIB_NAME)
101+
102+
# set the main app
103+
$(APP_NAME) : $(MAIN)
104+
$(CC) $(CFLAGS) $^ $(INC_DIRS) $(LIBS) -o $@
105+
106+
# set the shared lib
107+
$(SHARED_LIB_NAME).$(VERSION) : $(SRCS)
108+
$(CC) $(CFLAGS) -shared -fPIC $^ $(INC_DIRS) -lm -o $@
109+
110+
# set the *.o for the static libraries
111+
OBJS := $(patsubst %.c, %.o, $(SRCS))
112+
113+
# set the static lib
114+
$(STATIC_LIB_NAME) : $(OBJS)
115+
$(AR) -r $@ $^
116+
117+
# .*o used by the static lib
118+
$(OBJS) : $(SRCS)
119+
$(CC) $(CFLAGS) -c $^ $(INC_DIRS) -o $@
120+
121+
# set install .PHONY
122+
.PHONY: install
123+
124+
# install files
125+
install :
126+
$(shell if [ ! -d $(INC_INSTL_DIRS) ]; then mkdir $(INC_INSTL_DIRS); fi;)
127+
cp -rv $(INSTL_INCS) $(INC_INSTL_DIRS)
128+
cp -rv $(SHARED_LIB_NAME).$(VERSION) $(LIB_INSTL_DIRS)
129+
ln -sf $(LIB_INSTL_DIRS)/$(SHARED_LIB_NAME).$(VERSION) $(LIB_INSTL_DIRS)/$(SHARED_LIB_NAME)
130+
cp -rv $(STATIC_LIB_NAME) $(LIB_INSTL_DIRS)
131+
cp -rv $(APP_NAME) $(BIN_INSTL_DIRS)
132+
133+
# set install .PHONY
134+
.PHONY: uninstall
135+
136+
# uninstall files
137+
uninstall :
138+
rm -rf $(INC_INSTL_DIRS)
139+
rm -rf $(LIB_INSTL_DIRS)/$(SHARED_LIB_NAME).$(VERSION)
140+
rm -rf $(LIB_INSTL_DIRS)/$(SHARED_LIB_NAME)
141+
rm -rf $(LIB_INSTL_DIRS)/$(STATIC_LIB_NAME)
142+
rm -rf $(BIN_INSTL_DIRS)/$(APP_NAME)
143+
144+
# set clean .PHONY
145+
.PHONY: clean
146+
147+
# clean the project
148+
clean :
149+
rm -rf $(APP_NAME) $(SHARED_LIB_NAME).$(VERSION) $(STATIC_LIB_NAME)

0 commit comments

Comments
 (0)