Skip to content

Commit

Permalink
feat: add cmake
Browse files Browse the repository at this point in the history
  • Loading branch information
libdriver committed Sep 26, 2022
1 parent 4f718ba commit 9ed6dcd
Show file tree
Hide file tree
Showing 8 changed files with 552 additions and 48 deletions.
212 changes: 212 additions & 0 deletions project/raspberrypi4b/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
#
# Copyright (c) 2015 - present LibDriver All rights reserved
#
# The MIT License (MIT)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#

# set the cmake minimum version
cmake_minimum_required(VERSION 3.0)

# set the project name and language
project(w25qxx C)

# read the version from files
file(READ ${CMAKE_CURRENT_SOURCE_DIR}/cmake/VERSION ${CMAKE_PROJECT_NAME}_VERSION)

# set the project version
set(PROJECT_VERSION ${${CMAKE_PROJECT_NAME}_VERSION})

# set c standard c99
set(CMAKE_C_STANDARD 99)

# enable c standard required
set(CMAKE_C_STANDARD_REQUIRED True)

# set release level
set(CMAKE_BUILD_TYPE Release)

# set the release flags of c
set(CMAKE_C_FLAGS_RELEASE "-O3 -DNDEBUG")

# include cmake package config helpers
include(CMakePackageConfigHelpers)

# find the pkgconfig and use this tool to find the third party packages
find_package(PkgConfig REQUIRED)

# find the third party packages with pkgconfig
pkg_search_module(GPIOD REQUIRED libgpiod)

# include all library header directories
set(LIB_INC_DIRS
${GPIOD_INCLUDE_DIRS}
)

# include all linked libraries
set(LIBS
${GPIOD_LIBRARIES}
)

# include all header directories
set(INC_DIRS
${LIB_INC_DIRS}
${CMAKE_CURRENT_SOURCE_DIR}/../../src
${CMAKE_CURRENT_SOURCE_DIR}/../../interface
${CMAKE_CURRENT_SOURCE_DIR}/../../example
${CMAKE_CURRENT_SOURCE_DIR}/../../test
${CMAKE_CURRENT_SOURCE_DIR}/interface/inc
)

# include all installed headers
file(GLOB INSTL_INCS
${CMAKE_CURRENT_SOURCE_DIR}/../../src/*.h
)

# include all sources files
file(GLOB SRCS
${CMAKE_CURRENT_SOURCE_DIR}/../../src/*.c
)

# include executable source
file(GLOB MAIN
${SRCS}
${CMAKE_CURRENT_SOURCE_DIR}/../../example/*.c
${CMAKE_CURRENT_SOURCE_DIR}/../../test/*.c
${CMAKE_CURRENT_SOURCE_DIR}/interface/src/*.c
${CMAKE_CURRENT_SOURCE_DIR}/driver/src/*.c
${CMAKE_CURRENT_SOURCE_DIR}/src/main.c
)

# enable output as a static library
add_library(${CMAKE_PROJECT_NAME}_static STATIC ${SRCS})

# set the static library include directories
target_include_directories(${CMAKE_PROJECT_NAME}_static PRIVATE ${INC_DIRS})

# set the static library link libraries
target_link_libraries(${CMAKE_PROJECT_NAME}_static
m
)

# rename as ${CMAKE_PROJECT_NAME}
set_target_properties(${CMAKE_PROJECT_NAME}_static PROPERTIES OUTPUT_NAME ${CMAKE_PROJECT_NAME})

# don't delete ${CMAKE_PROJECT_NAME} libs
set_target_properties(${CMAKE_PROJECT_NAME}_static PROPERTIES CLEAN_DIRECT_OUTPUT 1)

# set the static library version
set_target_properties(${CMAKE_PROJECT_NAME}_static PROPERTIES VERSION ${${CMAKE_PROJECT_NAME}_VERSION})

# enable output as a dynamic library
add_library(${CMAKE_PROJECT_NAME} SHARED ${SRCS})

# set the executable program include directories
target_include_directories(${CMAKE_PROJECT_NAME}
PUBLIC $<INSTALL_INTERFACE:include/${CMAKE_PROJECT_NAME}>
PRIVATE ${INC_DIRS}
)

# set the dynamic library link libraries
target_link_libraries(${CMAKE_PROJECT_NAME}
m
)

# rename as ${CMAKE_PROJECT_NAME}
set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES OUTPUT_NAME ${CMAKE_PROJECT_NAME})

# don't delete ${CMAKE_PROJECT_NAME} libs
set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES CLEAN_DIRECT_OUTPUT 1)

# include the public header
set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES PUBLIC_HEADER "${INSTL_INCS}")

# set the dynamic library version
set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES VERSION ${${CMAKE_PROJECT_NAME}_VERSION})

# enable the executable program
add_executable(${CMAKE_PROJECT_NAME}_exe ${MAIN})

# set the executable program include directories
target_include_directories(${CMAKE_PROJECT_NAME}_exe PRIVATE ${INC_DIRS})

# set the executable program link libraries
target_link_libraries(${CMAKE_PROJECT_NAME}_exe
${LIBS}
m
pthread
)

# rename as ${CMAKE_PROJECT_NAME}
set_target_properties(${CMAKE_PROJECT_NAME}_exe PROPERTIES OUTPUT_NAME ${CMAKE_PROJECT_NAME})

# don't delete ${CMAKE_PROJECT_NAME} exe
set_target_properties(${CMAKE_PROJECT_NAME}_exe PROPERTIES CLEAN_DIRECT_OUTPUT 1)

# install the binary
install(TARGETS ${CMAKE_PROJECT_NAME}_exe
RUNTIME DESTINATION bin
)

# install the static library
install(TARGETS ${CMAKE_PROJECT_NAME}_static
ARCHIVE DESTINATION lib
)

# install the dynamic library
install(TARGETS ${CMAKE_PROJECT_NAME}
EXPORT ${CMAKE_PROJECT_NAME}-targets
LIBRARY DESTINATION lib
PUBLIC_HEADER DESTINATION include/${CMAKE_PROJECT_NAME}
)

# make the cmake config file
configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/config.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/cmake/${CMAKE_PROJECT_NAME}-config.cmake
INSTALL_DESTINATION cmake
)

# write the cmake config version
write_basic_package_version_file(${CMAKE_CURRENT_BINARY_DIR}/cmake/${CMAKE_PROJECT_NAME}-config-version.cmake
VERSION ${PACKAGE_VERSION}
COMPATIBILITY AnyNewerVersion
)

# install the cmake files
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/cmake/${CMAKE_PROJECT_NAME}-config.cmake"
"${CMAKE_CURRENT_BINARY_DIR}/cmake/${CMAKE_PROJECT_NAME}-config-version.cmake"
DESTINATION cmake
)

# set the export items
install(EXPORT ${CMAKE_PROJECT_NAME}-targets
DESTINATION cmake
)

# add uninstall command
add_custom_target(uninstall
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/uninstall.cmake
)

#include ctest module
include(CTest)

# creat a test
add_test(NAME ${CMAKE_PROJECT_NAME}_test COMMAND ${CMAKE_PROJECT_NAME}_exe -p)
163 changes: 148 additions & 15 deletions project/raspberrypi4b/Makefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,149 @@
CC := gcc
SRC := $(wildcard ./interface/src/*.c) \
$(wildcard ./driver/src/*.c) \
$(wildcard ./src/*.c) \
$(wildcard ../../src/*.c) \
$(wildcard ../../test/*.c) \
$(wildcard ../../example/*.c)
LIBS := -lm
#
# Copyright (c) 2015 - present LibDriver All rights reserved
#
# The MIT License (MIT)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#

# set the project version
VERSION := 1.0.0

# set the application name
APP_NAME := w25qxx

# set the shared libraries name
SHARED_LIB_NAME := libw25qxx.so

# set the static libraries name
STATIC_LIB_NAME := libw25qxx.a

# set the install directories
INSTL_DIRS := /usr/local

# set the include directories
INC_INSTL_DIRS := $(INSTL_DIRS)/include/$(APP_NAME)

# set the library directories
LIB_INSTL_DIRS := $(INSTL_DIRS)/lib

# set the bin directories
BIN_INSTL_DIRS := $(INSTL_DIRS)/bin

# set the compiler
CC := gcc

# set the ar tool
AR := ar

# set the packages name
PKGS := libgpiod

# set the pck-config header directories
LIB_INC_DIRS := $(shell pkg-config --cflags $(PKGS))

# set the linked libraries
LIBS := -lm \
-lpthread

# add the linked libraries
LIBS += $(shell pkg-config --libs $(PKGS))

# set all header directories
INC_DIRS := -I ../../src/ \
-I ../../interface/ \
-I ../../example/ \
-I ../../test/ \
-I ./interface/inc/

# add the linked libraries header directories
INC_DIRS += $(LIB_INC_DIRS)

# set the installing headers
INSTL_INCS := $(wildcard ../../src/*.h)

# set all sources files
SRCS := $(wildcard ../../src/*.c)

# set the main source
MAIN := $(SRCS) \
$(wildcard ../../example/*.c) \
$(wildcard ../../test/*.c) \
$(wildcard ./interface/src/*.c) \
$(wildcard ./driver/src/*.c) \
$(wildcard ./src/main.c)

# set flags of the compiler
CFLAGS := -O3 \
-I ./interface/inc/ \
-I ../../interface/ \
-I ../../src/ \
-I ../../test/ \
-I ../../example/
w25qxx : $(SRC)
"$(CC)" $(CFLAGS) $^ $(LIBS) -o $@
-DNDEBUG

# set all .PHONY
.PHONY: all

# set the output list
all: $(APP_NAME) $(SHARED_LIB_NAME).$(VERSION) $(STATIC_LIB_NAME)

# set the main app
$(APP_NAME) : $(MAIN)
$(CC) $(CFLAGS) $^ $(INC_DIRS) $(LIBS) -o $@

# set the shared lib
$(SHARED_LIB_NAME).$(VERSION) : $(SRCS)
$(CC) $(CFLAGS) -shared -fPIC $^ $(INC_DIRS) -lm -o $@

# set the *.o for the static libraries
OBJS := $(patsubst %.c, %.o, $(SRCS))

# set the static lib
$(STATIC_LIB_NAME) : $(OBJS)
$(AR) -r $@ $^

# .*o used by the static lib
$(OBJS) : $(SRCS)
$(CC) $(CFLAGS) -c $^ $(INC_DIRS) -o $@

# set install .PHONY
.PHONY: install

# install files
install :
$(shell if [ ! -d $(INC_INSTL_DIRS) ]; then mkdir $(INC_INSTL_DIRS); fi;)
cp -rv $(INSTL_INCS) $(INC_INSTL_DIRS)
cp -rv $(SHARED_LIB_NAME).$(VERSION) $(LIB_INSTL_DIRS)
ln -sf $(LIB_INSTL_DIRS)/$(SHARED_LIB_NAME).$(VERSION) $(LIB_INSTL_DIRS)/$(SHARED_LIB_NAME)
cp -rv $(STATIC_LIB_NAME) $(LIB_INSTL_DIRS)
cp -rv $(APP_NAME) $(BIN_INSTL_DIRS)

# set install .PHONY
.PHONY: uninstall

# uninstall files
uninstall :
rm -rf $(INC_INSTL_DIRS)
rm -rf $(LIB_INSTL_DIRS)/$(SHARED_LIB_NAME).$(VERSION)
rm -rf $(LIB_INSTL_DIRS)/$(SHARED_LIB_NAME)
rm -rf $(LIB_INSTL_DIRS)/$(STATIC_LIB_NAME)
rm -rf $(BIN_INSTL_DIRS)/$(APP_NAME)

# set clean .PHONY
.PHONY: clean

# clean the project
clean :
rm -rf $(APP_NAME) $(SHARED_LIB_NAME).$(VERSION) $(STATIC_LIB_NAME)
Loading

0 comments on commit 9ed6dcd

Please sign in to comment.