From c1da233ab0fc6ac5bea9b1e3634ee7c2a32e4b32 Mon Sep 17 00:00:00 2001 From: Jianhui Zhao Date: Sat, 30 Dec 2017 12:24:00 +0800 Subject: [PATCH] Improve CMakeLists.txt Signed-off-by: Jianhui Zhao --- .gitignore | 6 ++++++ CMakeLists.txt | 10 +--------- cmake/Modules/FindLibubox.cmake | 9 ++------- cmake/Modules/FindLibuhttpd.cmake | 29 +++++++++++++++++++++++++++++ example/CMakeLists.txt | 17 ++++++++++++++++- src/CMakeLists.txt | 8 ++++++-- 6 files changed, 60 insertions(+), 19 deletions(-) create mode 100755 cmake/Modules/FindLibuhttpd.cmake diff --git a/.gitignore b/.gitignore index 7cf650c..8550171 100644 --- a/.gitignore +++ b/.gitignore @@ -55,3 +55,9 @@ server-cert.pem server-key.pem build/ + +CMakeFiles +CMakeCache.txt +cmake_install.cmake +Makefile +helloworld diff --git a/CMakeLists.txt b/CMakeLists.txt index 71592db..9f03f31 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,17 +1,9 @@ cmake_minimum_required(VERSION 2.8) -project(libuhttp C) +project(libuhttpd C) list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/Modules/") #set(CMAKE_VERBOSE_MAKEFILE ON) -add_definitions(-O -Wall -Werror --std=gnu99 -D_GNU_SOURCE -Wno-misleading-indentation) - -include_directories( - ${CMAKE_CURRENT_SOURCE_DIR}/src/ - ${CMAKE_CURRENT_BINARY_DIR}/src/ -) - add_subdirectory(src) -add_subdirectory(example) diff --git a/cmake/Modules/FindLibubox.cmake b/cmake/Modules/FindLibubox.cmake index f68decf..100f253 100755 --- a/cmake/Modules/FindLibubox.cmake +++ b/cmake/Modules/FindLibubox.cmake @@ -1,8 +1,8 @@ # - Try to find libubox # Once done this will define # LIBUBOX_FOUND - System has libubox -# LIBUBOX_INCLUDE_DIRS - The libubox include directories -# LIBUBOX_LIBRARIES - The libraries needed to use libubox +# LIBUBOX_INCLUDE_DIR - The libubox include directories +# LIBUBOX_LIBRARY - The libraries needed to use libubox find_path(LIBUBOX_INCLUDE_DIR uloop.h PATH_SUFFIXES libubox) find_library(LIBUBOX_LIBRARY ubox PATH_SUFFIXES lib64) @@ -14,9 +14,4 @@ find_package_handle_standard_args(Libubox REQUIRED_VARS LIBUBOX_LIBRARY LIBUBOX_INCLUDE_DIR VERSION_VAR LIBUBOX_VERSION) -if(LIBUBOX_FOUND) - set(LIBUBOX_LIBRARIES ${LIBUBOX_LIBRARY}) - set(LIBUBOX_INCLUDE_DIRS ${LIBUBOX_INCLUDE_DIR}) -endif() - mark_as_advanced(LIBUBOX_INCLUDE_DIR LIBUBOX_LIBRARY) diff --git a/cmake/Modules/FindLibuhttpd.cmake b/cmake/Modules/FindLibuhttpd.cmake new file mode 100755 index 0000000..877d65e --- /dev/null +++ b/cmake/Modules/FindLibuhttpd.cmake @@ -0,0 +1,29 @@ +# - Try to find libuhttpd +# Once done this will define +# LIBUHTTPD_FOUND - System has libuhttpd +# LIBUHTTPD_INCLUDE_DIR - The libuhttpd include directories +# LIBUHTTPD_LIBRARY - The libraries needed to use libuhttpd + +find_path(LIBUHTTPD_INCLUDE_DIR uhttpd.h PATH_SUFFIXES uhttpd) +find_library(LIBUHTTPD_LIBRARY uhttpd PATH_SUFFIXES lib64) + +if(LIBUHTTPD_INCLUDE_DIR) + file(STRINGS "${LIBUHTTPD_INCLUDE_DIR}/uhttpd.h" + LIBUHTTPD_VERSION_MAJOR REGEX "^#define[ \t]+UHTTPD_VERSION_MAJOR[ \t]+[0-9]+") + file(STRINGS "${LIBUHTTPD_INCLUDE_DIR}/uhttpd.h" + LIBUHTTPD_VERSION_MINOR REGEX "^#define[ \t]+UHTTPD_VERSION_MINOR[ \t]+[0-9]+") + string(REGEX REPLACE "[^0-9]+" "" LIBUHTTPD_VERSION_MAJOR "${LIBUHTTPD_VERSION_MAJOR}") + string(REGEX REPLACE "[^0-9]+" "" LIBUHTTPD_VERSION_MINOR "${LIBUHTTPD_VERSION_MINOR}") + set(LIBUHTTPD_VERSION "${LIBUHTTPD_VERSION_MAJOR}.${LIBUHTTPD_VERSION_MINOR}") + unset(LIBUHTTPD_VERSION_MINOR) + unset(LIBUHTTPD_VERSION_MAJOR) +endif() + +include(FindPackageHandleStandardArgs) +# handle the QUIETLY and REQUIRED arguments and set LIBUHTTPD_FOUND to TRUE +# if all listed variables are TRUE and the requested version matches. +find_package_handle_standard_args(Libuhttpd REQUIRED_VARS + LIBUHTTPD_LIBRARY LIBUHTTPD_INCLUDE_DIR + VERSION_VAR LIBUHTTPD_VERSION) + +mark_as_advanced(LIBUHTTPD_INCLUDE_DIR LIBUHTTPD_LIBRARY) diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index ee506f0..5432f45 100755 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -1,2 +1,17 @@ +cmake_minimum_required(VERSION 2.8) + +project(libuhttpd-example C) + +#set(CMAKE_VERBOSE_MAKEFILE ON) + +list(APPEND CMAKE_MODULE_PATH "../cmake/Modules/") + +add_definitions(-O -Wall -Werror --std=gnu99 -D_GNU_SOURCE -Wno-misleading-indentation) + +find_package(Libuhttpd REQUIRED) +find_package(Libubox REQUIRED) + +include_directories(${LIBUHTTPD_INCLUDE_DIR}) + add_executable(helloworld helloworld.c) -target_link_libraries(helloworld uhttpd) +target_link_libraries(helloworld ${LIBUHTTPD_LIBRARY} ${LIBUBOX_LIBRARY}) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f329012..df244e0 100755 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,3 +1,6 @@ + +add_definitions(-O -Wall -Werror --std=gnu99 -D_GNU_SOURCE -Wno-misleading-indentation) + # The version number. set(UHTTPD_VERSION_MAJOR 1) set(UHTTPD_VERSION_MINOR 0) @@ -6,8 +9,9 @@ set(UHTTPD_VERSION_PATCH 1) # Check the third party Libraries find_package(Libubox REQUIRED) -include_directories(${LIBUBOX_INCLUDE_DIR} ${LUA_INCLUDE_DIR}) -set(EXTRA_LIBS ${LIBUBOX_LIBRARIES} ${LUA_LIBRARIES} dl) +include_directories(${CMAKE_CURRENT_BINARY_DIR} ${LIBUBOX_INCLUDE_DIR}) + +set(EXTRA_LIBS ${LIBUBOX_LIBRARY} dl) set(SOURCE_FILES uhttpd.c client.c log.c utils.c file.c action.c) set(UHTTPD_DEBUG_CONFIG 0)