build: refactor config infrastructure and test build setup

* Configuration is now done with files, instead of through command line
  switches specified in CMakeLists.txt.
* Link tests with a static library instead of compiling individual
  wepoll source files as part of the test executable. This should make
  the CI run significantly faster.
This commit is contained in:
Bert Belder 2020-01-12 23:45:03 +01:00
parent 4536518f8a
commit 3ad20d71ff
No known key found for this signature in database
GPG Key ID: 7A77887B2E2ED461
24 changed files with 149 additions and 89 deletions

1
.gitignore vendored
View File

@ -37,6 +37,7 @@
/CMakeScripts/ /CMakeScripts/
/Debug/ /Debug/
/dist/ /dist/
/gen/
/ipch/ /ipch/
/lib/ /lib/
/MinSizeRel/ /MinSizeRel/

View File

@ -1,6 +1,8 @@
cmake_minimum_required(VERSION 3.0.0) cmake_minimum_required(VERSION 3.0.0)
project(wepoll) project(wepoll)
include(CMakeParseArguments)
link_libraries(ws2_32) link_libraries(ws2_32)
if(MSVC) if(MSVC)
@ -15,91 +17,148 @@ endif()
# Work around bug in in Windows SDK 10.0.17763.0 and maybe other versions. # Work around bug in in Windows SDK 10.0.17763.0 and maybe other versions.
add_definitions(-D_CRT_HAS_CXX17=0) add_definitions(-D_CRT_HAS_CXX17=0)
set(DIST_DIR "dist") file(GLOB SOURCES_DOC LICENSE *.md)
set(TEST_RUNNER "tools/run-tests.js") file(GLOB_RECURSE SOURCES_CONFIG config/*.h)
file(GLOB SOURCES_CONFIG_EXTERNAL_DLLEXPORT config/external/dllexport/*.h)
file(GLOB SOURCES_HEADER include/*.h) file(GLOB SOURCES_CONFIG_EXTERNAL_STATIC config/external/static/*.h)
file(GLOB SOURCES_CONFIG_INTERNAL_BUNDLE config/internal/bundle/*.h)
file(GLOB SOURCES_CONFIG_INTERNAL_DEFAULT config/internal/default/*.h)
file(GLOB SOURCES_INCLUDE include/*.h)
file(GLOB SOURCES_SRC src/*.c src/*.h) file(GLOB SOURCES_SRC src/*.c src/*.h)
file(GLOB SOURCES_SRC_C src/*.c) file(GLOB SOURCES_SRC_C src/*.c)
file(GLOB SOURCES_SRC_REGULAR src/regular/*.c src/regular/*.h)
file(GLOB SOURCES_SRC_BUNDLE src/bundle/*.c src/bundle/*.h)
file(GLOB SOURCES_TEST test/*.c) file(GLOB SOURCES_TEST test/*.c)
file(GLOB SOURCES_TEST_SHARED test/shared/*.c test/shared/*.h) file(GLOB SOURCES_TEST_SHARED test/shared/*.c test/shared/*.h)
file(GLOB SOURCES_DOC LICENSE *.md)
get_filename_component(HEADER_NAME ${SOURCES_HEADER} NAME_WE)
string(TOUPPER ${HEADER_NAME} HEADER_NAME_UC)
set(BUNDLE_HEADER "${DIST_DIR}/${HEADER_NAME}.h")
set(BUNDLE_SRC "${DIST_DIR}/${HEADER_NAME}.c")
set(DLL_NAME "${HEADER_NAME}")
set(BUNDLE_DLL_NAME "${HEADER_NAME}-bundle")
set_property(GLOBAL PROPERTY USE_FOLDERS ON) set_property(GLOBAL PROPERTY USE_FOLDERS ON)
source_group("" FILES ${SOURCES_HEADER}) source_group(doc FILES ${SOURCES_DOC})
source_group(config FILES ${SOURCES_CONFIG})
source_group(include FILES ${SOURCES_INCLUDE})
source_group(src FILES ${SOURCES_SRC}) source_group(src FILES ${SOURCES_SRC})
source_group(src FILES ${SOURCES_SRC_REGULAR})
source_group(src FILES ${SOURCES_SRC_BUNDLE})
source_group("" FILES ${SOURCES_TEST}) source_group("" FILES ${SOURCES_TEST})
source_group(test/shared FILES ${SOURCES_TEST_SHARED}) source_group(test/shared FILES ${SOURCES_TEST_SHARED})
source_group(doc FILES ${SOURCES_DOC})
source_group(${DIST_DIR} FILES ${BUNDLE_HEADER} ${BUNDLE_SRC})
add_custom_command( get_filename_component(LIB_NAME ${SOURCES_INCLUDE} NAME_WE)
OUTPUT ${BUNDLE_HEADER}
COMMAND node tools/bundle.js ${SOURCES_HEADER} > ${BUNDLE_HEADER}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
IMPLICIT_DEPENDS c ${SOURCES_HEADER}
)
add_custom_command( set(TOOL_RUN_TESTS "tools/run-tests.js")
OUTPUT ${BUNDLE_SRC} set(TOOL_BUNDLE "tools/bundle.js")
COMMAND node tools/bundle.js -Iinclude -Isrc -Isrc/bundle --strip-guards ${SOURCES_HEADER} ${SOURCES_SRC_C} > ${BUNDLE_SRC}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} set(DIST_DIR "${PROJECT_BINARY_DIR}/dist")
IMPLICIT_DEPENDS c ${SOURCES_HEADER} ${SOURCES_SRC} ${SOURCES_SRC_BUNDLE} set(GEN_DIR "${PROJECT_BINARY_DIR}/gen")
)
add_custom_target(dist DEPENDS ${BUNDLE_SRC} ${BUNDLE_HEADER})
file(MAKE_DIRECTORY ${DIST_DIR}) file(MAKE_DIRECTORY ${DIST_DIR})
file(MAKE_DIRECTORY ${GEN_DIR})
function(bundle_header)
cmake_parse_arguments(BUNDLE "" "OUTPUT;EXTERNAL_CONFIG" "" ${ARGN})
set(CONFIG_EXTERNAL_DIR "config/external/${BUNDLE_EXTERNAL_CONFIG}")
file(GLOB SOURCES_CONFIG_EXTERNAL "${CONFIG_EXTERNAL_DIR}/*.h")
add_custom_command( add_custom_command(
TARGET dist POST_BUILD OUTPUT ${BUNDLE_OUTPUT}
COMMAND node ${TOOL_BUNDLE}
-I${CONFIG_EXTERNAL_DIR}
${SOURCES_INCLUDE}
> ${BUNDLE_OUTPUT}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
DEPENDS ${TOOL_BUNDLE} ${SOURCES_CONFIG_EXTERNAL} ${SOURCES_INCLUDE}
)
endfunction()
function(bundle_source)
cmake_parse_arguments(BUNDLE "" "OUTPUT;EXTERNAL_CONFIG" "" ${ARGN})
set(CONFIG_EXTERNAL_DIR "config/external/${BUNDLE_EXTERNAL_CONFIG}")
file(GLOB SOURCES_CONFIG_EXTERNAL "${CONFIG_EXTERNAL_DIR}/*.h")
add_custom_command(
OUTPUT ${BUNDLE_OUTPUT}
COMMAND node ${TOOL_BUNDLE} --strip-guards
-I${CONFIG_EXTERNAL_DIR} -Iconfig/internal/bundle -Iinclude -Isrc
${SOURCES_INCLUDE} ${SOURCES_SRC_C}
> ${BUNDLE_OUTPUT}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
DEPENDS ${TOOL_BUNDLE}
${SOURCES_CONFIG_EXTERNAL} ${SOURCES_CONFIG_INTERNAL_BUNDLE}
${SOURCES_INCLUDE} ${SOURCES_SRC}
)
endfunction()
set(BUNDLE_DIST_TARGET "dist")
set(BUNDLE_DIST_HEADER "${DIST_DIR}/${LIB_NAME}.h")
set(BUNDLE_DIST_SOURCE "${DIST_DIR}/${LIB_NAME}.c")
bundle_header(OUTPUT ${BUNDLE_DIST_HEADER} EXTERNAL_CONFIG static)
bundle_source(OUTPUT ${BUNDLE_DIST_SOURCE} EXTERNAL_CONFIG static)
set_source_files_properties(
${BUNDLE_DIST_HEADER} ${BUNDLE_DIST_SOURCE}
PROPERTIES GENERATED TRUE
)
add_custom_target(
${BUNDLE_DIST_TARGET}
DEPENDS ${BUNDLE_DIST_SOURCE} ${BUNDLE_DIST_HEADER}
SOURCES ${SOURCES_CONFIG_EXTERNAL_STATIC} ${SOURCES_CONFIG_INTERNAL_BUNDLE}
${SOURCES_INCLUDE} ${SOURCES_SRC} ${SOURCES_DOC}
)
add_custom_command(
TARGET ${BUNDLE_DIST_TARGET} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${SOURCES_DOC} ${DIST_DIR} COMMAND ${CMAKE_COMMAND} -E copy_if_different ${SOURCES_DOC} ${DIST_DIR}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
DEPENDS ${SOURCES_DOC} DEPENDS ${SOURCES_DOC}
) )
set(DLL_TARGET "${HEADER_NAME}.dll") set(DLL_TARGET "${LIB_NAME}.dll")
add_library(${DLL_TARGET} SHARED ${SOURCES_HEADER} ${SOURCES_SRC} ${SOURCES_SRC_REGULAR}) add_library(
target_include_directories(${DLL_TARGET} PUBLIC include src/regular) ${DLL_TARGET} SHARED
if(MSVC) ${SOURCES_CONFIG_EXTERNAL_DLLEXPORT} ${SOURCES_CONFIG_INTERNAL_DEFAULT}
target_compile_definitions(${DLL_TARGET} PUBLIC "-D${HEADER_NAME_UC}_EXPORT=__declspec(dllexport)" ) ${SOURCES_INCLUDE} ${SOURCES_SRC}
else() )
target_compile_options(${DLL_TARGET} PUBLIC -fvisibility=hidden) target_include_directories(
target_compile_definitions(${DLL_TARGET} PUBLIC "-D${HEADER_NAME_UC}_EXPORT=__attribute__((visibility(\"default\")))") ${DLL_TARGET} PUBLIC
endif() config/external/dllexport config/internal/default include src
set_target_properties(${DLL_TARGET} PROPERTIES OUTPUT_NAME ${DLL_NAME}) )
set_target_properties(${DLL_TARGET} PROPERTIES OUTPUT_NAME ${DLL_TARGET})
set(BUNDLE_DLL_TARGET "${BUNDLE_DLL_NAME}.dll") set(BUNDLE_DLL_TARGET "${LIB_NAME}-bundle.dll")
add_library(${BUNDLE_DLL_TARGET} SHARED ${BUNDLE_HEADER} ${BUNDLE_SRC}) set(BUNDLE_DLL_SOURCE "${GEN_DIR}/${LIB_NAME}-bundle-dll.c")
if(MSVC) bundle_source(OUTPUT ${BUNDLE_DLL_SOURCE} EXTERNAL_CONFIG dllexport)
target_compile_definitions(${BUNDLE_DLL_TARGET} PUBLIC "-D${HEADER_NAME_UC}_EXPORT=__declspec(dllexport)" ) set_source_files_properties(${BUNDLE_DLL_SOURCE} PROPERTIES GENERATED TRUE)
else() add_library(
target_compile_definitions(${BUNDLE_DLL_TARGET} PUBLIC "-D${HEADER_NAME_UC}_EXPORT=__attribute__((visibility(\"default\")))") ${BUNDLE_DLL_TARGET} SHARED
endif() ${SOURCES_CONFIG_EXTERNAL_DLLEXPORT} ${SOURCES_CONFIG_INTERNAL_DEFAULT}
set_target_properties(${BUNDLE_DLL_TARGET} PROPERTIES OUTPUT_NAME ${BUNDLE_DLL_NAME}) ${BUNDLE_DLL_SOURCE}
)
set_target_properties(
${BUNDLE_DLL_TARGET}
PROPERTIES OUTPUT_NAME "${LIB_NAME}-bundle"
)
set(TESTING_LIB_TARGET "${LIB_NAME}-testing.lib")
add_library(
${TESTING_LIB_TARGET} STATIC
${SOURCES_CONFIG_EXTERNAL_STATIC} ${SOURCES_CONFIG_INTERNAL_DEFAULT}
${SOURCES_INCLUDE} ${SOURCES_SRC} ${SOURCES_TEST_SHARED}
)
target_include_directories(
${TESTING_LIB_TARGET} PUBLIC
config/external/static config/internal/default include src
)
set_target_properties(
${TESTING_LIB_TARGET} PROPERTIES
OUTPUT_NAME "${LIB_NAME}-testing"
)
foreach(TEST_SOURCE ${SOURCES_TEST}) foreach(TEST_SOURCE ${SOURCES_TEST})
get_filename_component(TEST_NAME ${TEST_SOURCE} NAME_WE) get_filename_component(TEST_NAME ${TEST_SOURCE} NAME_WE)
add_executable(${TEST_NAME} ${TEST_SOURCE} ${SOURCES_SRC} ${SOURCES_SRC_REGULAR} ${SOURCES_TEST_SHARED}) add_executable(${TEST_NAME} ${TEST_SOURCE})
target_include_directories(
${TEST_NAME} PUBLIC
config/external/static config/internal/default include src test/shared
)
target_link_libraries(${TEST_NAME} ${TESTING_LIB_TARGET})
set_target_properties(${TEST_NAME} PROPERTIES FOLDER Tests) set_target_properties(${TEST_NAME} PROPERTIES FOLDER Tests)
target_include_directories(${TEST_NAME} PUBLIC include src src/regular test/shared)
list(APPEND TEST_TARGETS ${TEST_NAME}) list(APPEND TEST_TARGETS ${TEST_NAME})
list(APPEND TEST_OUTPUTS $<TARGET_FILE:${TEST_NAME}>) list(APPEND TEST_OUTPUTS $<TARGET_FILE:${TEST_NAME}>)
endforeach(TEST_SOURCE ${SOURCES_TEST}) endforeach()
add_custom_target( add_custom_target(
test-all test-all
COMMAND node ${TEST_RUNNER} ${TEST_OUTPUTS} COMMAND node ${TOOL_RUN_TESTS} ${TEST_OUTPUTS}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
DEPENDS ${TEST_RUNNER} ${TEST_TARGETS} DEPENDS ${TOOL_RUN_TESTS} ${TEST_TARGETS}
) )

View File

@ -0,0 +1,5 @@
#ifdef _MSC_VER
#define WEPOLL_EXPORT __declspec(dllexport)
#else /* GCC/Clang */
#define WEPOLL_EXPORT __attribute__((dllexport))
#endif

View File

@ -0,0 +1 @@
#define WEPOLL_EXPORT

View File

@ -0,0 +1,2 @@
#define WEPOLL_INTERNAL static
#define WEPOLL_INTERNAL_VAR static

View File

@ -0,0 +1,2 @@
#define WEPOLL_INTERNAL
#define WEPOLL_INTERNAL_VAR extern

View File

@ -1,9 +1,7 @@
#ifndef WEPOLL_H_ #ifndef WEPOLL_H_
#define WEPOLL_H_ #define WEPOLL_H_
#ifndef WEPOLL_EXPORT #include "config-external.h"
#define WEPOLL_EXPORT
#endif
#include <stdint.h> #include <stdint.h>

View File

@ -1,7 +1,7 @@
#ifndef WEPOLL_AFD_H_ #ifndef WEPOLL_AFD_H_
#define WEPOLL_AFD_H_ #define WEPOLL_AFD_H_
#include "internal.h" #include "config.h"
#include "nt.h" #include "nt.h"
#include "win.h" #include "win.h"

View File

@ -1,7 +1,7 @@
#ifndef WEPOLL_API_H_ #ifndef WEPOLL_API_H_
#define WEPOLL_API_H_ #define WEPOLL_API_H_
#include "internal.h" #include "config.h"
WEPOLL_INTERNAL int epoll_global_init(void); WEPOLL_INTERNAL int epoll_global_init(void);

View File

@ -1,7 +0,0 @@
#ifndef WEPOLL_INTERNAL_H_
#define WEPOLL_INTERNAL_H_
#define WEPOLL_INTERNAL static
#define WEPOLL_INTERNAL_VAR static
#endif /* WEPOLL_INTERNAL_H_ */

6
src/config.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef WEPOLL_CONFIG_H_
#define WEPOLL_CONFIG_H_
#include "config-internal.h"
#endif /* WEPOLL_CONFIG_H_ */

View File

@ -1,7 +1,7 @@
#ifndef WEPOLL_ERROR_H_ #ifndef WEPOLL_ERROR_H_
#define WEPOLL_ERROR_H_ #define WEPOLL_ERROR_H_
#include "internal.h" #include "config.h"
#include "win.h" #include "win.h"
#define return_map_error(value) \ #define return_map_error(value) \

View File

@ -1,7 +1,7 @@
#ifndef WEPOLL_INIT_H_ #ifndef WEPOLL_INIT_H_
#define WEPOLL_INIT_H_ #define WEPOLL_INIT_H_
#include "internal.h" #include "config.h"
WEPOLL_INTERNAL int init(void); WEPOLL_INTERNAL int init(void);

View File

@ -1,7 +1,7 @@
#ifndef WEPOLL_NT_H_ #ifndef WEPOLL_NT_H_
#define WEPOLL_NT_H_ #define WEPOLL_NT_H_
#include "internal.h" #include "config.h"
#include "win.h" #include "win.h"
WEPOLL_INTERNAL int nt_global_init(void); WEPOLL_INTERNAL int nt_global_init(void);

View File

@ -1,7 +1,7 @@
#ifndef WEPOLL_POLL_GROUP_H_ #ifndef WEPOLL_POLL_GROUP_H_
#define WEPOLL_POLL_GROUP_H_ #define WEPOLL_POLL_GROUP_H_
#include "internal.h" #include "config.h"
#include "win.h" #include "win.h"
typedef struct poll_group poll_group_t; typedef struct poll_group poll_group_t;

View File

@ -1,7 +1,7 @@
#ifndef WEPOLL_PORT_H_ #ifndef WEPOLL_PORT_H_
#define WEPOLL_PORT_H_ #define WEPOLL_PORT_H_
#include "internal.h" #include "config.h"
#include "wepoll.h" #include "wepoll.h"
#include "win.h" #include "win.h"

View File

@ -3,7 +3,7 @@
#include <stdbool.h> #include <stdbool.h>
#include "internal.h" #include "config.h"
typedef struct queue_node queue_node_t; typedef struct queue_node queue_node_t;

View File

@ -19,7 +19,7 @@
* should use another lock to guarantee that this can't happen. * should use another lock to guarantee that this can't happen.
*/ */
#include "internal.h" #include "config.h"
typedef struct reflock { typedef struct reflock {
volatile long state; /* 32-bit Interlocked APIs operate on `long` values. */ volatile long state; /* 32-bit Interlocked APIs operate on `long` values. */

View File

@ -1,7 +0,0 @@
#ifndef WEPOLL_INTERNAL_H_
#define WEPOLL_INTERNAL_H_
#define WEPOLL_INTERNAL
#define WEPOLL_INTERNAL_VAR extern
#endif /* WEPOLL_INTERNAL_H_ */

View File

@ -1,7 +1,7 @@
#ifndef WEPOLL_SOCK_H_ #ifndef WEPOLL_SOCK_H_
#define WEPOLL_SOCK_H_ #define WEPOLL_SOCK_H_
#include "internal.h" #include "config.h"
#include "nt.h" #include "nt.h"
#include "wepoll.h" #include "wepoll.h"
#include "win.h" #include "win.h"

View File

@ -3,7 +3,7 @@
#include <stdint.h> #include <stdint.h>
#include "internal.h" #include "config.h"
#include "reflock.h" #include "reflock.h"
#include "tree.h" #include "tree.h"
#include "win.h" #include "win.h"

View File

@ -4,7 +4,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include "internal.h" #include "config.h"
/* N.b.: the tree functions do not set errno or LastError when they fail. Each /* N.b.: the tree functions do not set errno or LastError when they fail. Each
* of the API functions has at most one failure mode. It is up to the caller to * of the API functions has at most one failure mode. It is up to the caller to

View File

@ -1,7 +1,7 @@
#ifndef WEPOLL_WS_H_ #ifndef WEPOLL_WS_H_
#define WEPOLL_WS_H_ #define WEPOLL_WS_H_
#include "internal.h" #include "config.h"
#include "win.h" #include "win.h"
WEPOLL_INTERNAL int ws_global_init(void); WEPOLL_INTERNAL int ws_global_init(void);

View File

@ -12,13 +12,13 @@
#ifdef _MSC_VER #ifdef _MSC_VER
#define no_return __declspec(noreturn) #define no_return __declspec(noreturn)
#else /* GCC/clang */ #else /* GCC/Clang */
#define no_return __attribute__((noreturn)) #define no_return __attribute__((noreturn))
#endif #endif
#ifdef _MSC_VER #ifdef _MSC_VER
#define no_inline __declspec(noinline) #define no_inline __declspec(noinline)
#else /* GCC/clang */ #else /* GCC/Clang */
#define no_inline __attribute__((noinline)) #define no_inline __attribute__((noinline))
#endif #endif