Skip to content

Commit

Permalink
add single header build
Browse files Browse the repository at this point in the history
  • Loading branch information
goatshriek committed Oct 7, 2023
1 parent 64c036e commit 279d7cb
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 30 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ string(CONCAT enable_cpp_help_string
option(ENABLE_CPP ${enable_cpp_help_string} OFF)

set(SINGLE_SOURCE_FILE "${PROJECT_BINARY_DIR}/stumpless.c")
set(SINGLE_HEADER_FILE "${PROJECT_BINARY_DIR}/stumpless.h")
option(BUILD_SINGLE_FILE "create a single file version of library" OFF)
option(BUILD_PYTHON "include the python libary" OFF)

Expand Down
7 changes: 4 additions & 3 deletions tools/cmake/example.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function(add_no_run_example name)
target_include_directories(example-${name}
PRIVATE
${PROJECT_SOURCE_DIR}/include
${CMAKE_BINARY_DIR}/include
${PROJECT_BINARY_DIR}/include
)
endfunction(add_no_run_example)

Expand All @@ -42,8 +42,9 @@ function(add_no_run_single_file_example name)

target_include_directories(example-single-file-${name}
PRIVATE
${PROJECT_SOURCE_DIR}/include
${CMAKE_BINARY_DIR}/include
${PROJECT_BINARY_DIR}
#${PROJECT_SOURCE_DIR}/include
#${CMAKE_BINARY_DIR}/include
)
endfunction(add_no_run_single_file_example)

Expand Down
75 changes: 65 additions & 10 deletions tools/cmake/single_file.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
message("creating single file library ${SINGLE_SOURCE_FILE}")

function(include_file source_filename include_filenames already_included)
function(private_include_file source_filename include_filenames already_included)
foreach(include_filename ${include_filenames})
file(APPEND "${source_filename}" "\n/* ${include_filename} */\n\n")
file(STRINGS "${include_filename}" raw_include_contents NEWLINE_CONSUME ENCODING UTF-8)
Expand All @@ -9,16 +7,20 @@ function(include_file source_filename include_filenames already_included)
string(REPLACE "]" "@CLOSE_BRACKET@" sanitized_include_contents "${sanitized_include_contents}")
string(REPLACE "\n" ";" file_lines "${sanitized_include_contents}")
foreach(line ${file_lines})
if("${line}" MATCHES ".*include \"(private/.+\.h)\"$")
if("${line}" MATCHES ".*include .((private|stumpless)/.+\.h).$")
if("${CMAKE_MATCH_1}" STREQUAL "private/config.h")
set(extracted_full_path "${PROJECT_BINARY_DIR}/include/private/config.h")
elseif("${CMAKE_MATCH_1}" STREQUAL "stumpless/config.h")
set(extracted_full_path "${PROJECT_BINARY_DIR}/include/stumpless/config.h")
elseif("${CMAKE_MATCH_1}" STREQUAL "stumpless/windows/default_events.h")
set(extracted_full_path "${PROJECT_BINARY_DIR}/include/stumpless/windows/default_events.h")
else()
set(extracted_full_path "${PROJECT_SOURCE_DIR}/include/${CMAKE_MATCH_1}")
endif()
list(FIND ${already_included} "${extracted_full_path}" FOUND_INDEX)
if(${FOUND_INDEX} EQUAL -1)
if(${FOUND_INDEX} EQUAL -1 AND EXISTS "${extracted_full_path}")
list(APPEND ${already_included} "${extracted_full_path}")
include_file(
private_include_file(
"${source_filename}"
"${extracted_full_path}"
${already_included}
Expand All @@ -41,22 +43,75 @@ function(include_file source_filename include_filenames already_included)
endforeach()
endfunction()

message("creating single file library ${SINGLE_SOURCE_FILE}")
file(WRITE "${SINGLE_SOURCE_FILE}" "")
set(include_list "")
foreach(source_file ${STUMPLESS_SOURCES})
if("${source_file}" MATCHES "\\.c$")
include_file(
private_include_file(
"${SINGLE_SOURCE_FILE}"
"${source_file}"
include_list
)
endif()
endforeach()

function(public_include_file source_filename include_filenames already_included)
foreach(include_filename ${include_filenames})
file(APPEND "${source_filename}" "\n/* ${include_filename} */\n\n")
file(STRINGS "${include_filename}" raw_include_contents NEWLINE_CONSUME ENCODING UTF-8)
string(REPLACE "\;" "@SEMICOLON@" sanitized_include_contents "${raw_include_contents}")
string(REPLACE "[" "@OPEN_BRACKET@" sanitized_include_contents "${sanitized_include_contents}")
string(REPLACE "]" "@CLOSE_BRACKET@" sanitized_include_contents "${sanitized_include_contents}")
string(REPLACE "\n" ";" file_lines "${sanitized_include_contents}")
foreach(line ${file_lines})
if("${line}" MATCHES ".*include <(stumpless/.+\.h)>$")
if("${CMAKE_MATCH_1}" STREQUAL "stumpless/config.h")
set(extracted_full_path "${PROJECT_BINARY_DIR}/include/stumpless/config.h")
elseif("${CMAKE_MATCH_1}" STREQUAL "stumpless/windows/default_events.h")
set(extracted_full_path "${PROJECT_BINARY_DIR}/include/stumpless/windows/default_events.h")
else()
set(extracted_full_path "${PROJECT_SOURCE_DIR}/include/${CMAKE_MATCH_1}")
endif()
list(FIND ${already_included} "${extracted_full_path}" FOUND_INDEX)
if(${FOUND_INDEX} EQUAL -1 AND EXISTS "${extracted_full_path}")
list(APPEND ${already_included} "${extracted_full_path}")
public_include_file(
"${source_filename}"
"${extracted_full_path}"
${already_included}
)
endif()
else()
set(prev_line "")
foreach(subline ${line})
if(NOT prev_line STREQUAL "")
file(APPEND "${source_filename}" "${prev_line}\\\n")
endif()
string(REPLACE "@CLOSE_BRACKET@" "]" original_subline "${subline}")
string(REPLACE "@OPEN_BRACKET@" "[" original_subline "${original_subline}")
string(REPLACE "@SEMICOLON@" ";" original_subline "${original_subline}")
set(prev_line "${original_subline}")
endforeach()
file(APPEND "${source_filename}" "${prev_line}\n")
endif()
endforeach()
endforeach()
endfunction()

message("creating single header ${SINGLE_HEADER_FILE}")
file(WRITE "${SINGLE_HEADER_FILE}" "")
set(include_list "")
public_include_file(
"${SINGLE_HEADER_FILE}"
"${PROJECT_SOURCE_DIR}/include/stumpless.h"
include_list
)

# this needs to happen after the stumpless library is set up
get_target_property(STUMPLESS_LINK_LIBRARIES stumpless LINK_LIBRARIES)
foreach(example_target ${STUMPLESS_SINGLE_FILE_TARGETS})
target_link_libraries(${example_target} ${STUMPLESS_LINK_LIBRARIES})
get_property(STUMPLESS_LINK_LIBRARIES TARGET stumpless PROPERTY LINK_LIBRARIES)
foreach(single_file_target ${STUMPLESS_SINGLE_FILE_TARGETS})
target_link_libraries(${single_file_target} ${STUMPLESS_LINK_LIBRARIES})
endforeach()

add_custom_target(bench-single-file
Expand Down
34 changes: 17 additions & 17 deletions tools/cmake/test.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function(private_add_function_test)
target_include_directories(function-test-${FUNCTION_TEST_ARG_NAME}
PRIVATE
${PROJECT_SOURCE_DIR}/include
${CMAKE_BINARY_DIR}/include
${PROJECT_BINARY_DIR}/include
)

add_test(NAME ${FUNCTION_TEST_ARG_NAME}
Expand Down Expand Up @@ -74,7 +74,7 @@ function(private_add_single_file_function_test)
target_include_directories(function-test-single-file-${FUNCTION_TEST_ARG_NAME}
PRIVATE
${PROJECT_SOURCE_DIR}/include
${CMAKE_BINARY_DIR}/include
${PROJECT_BINARY_DIR}/include
)
endfunction(private_add_single_file_function_test)

Expand Down Expand Up @@ -127,7 +127,7 @@ function(private_add_thread_safety_test)
target_include_directories(thread-safety-test-${THREAD_SAFETY_TEST_ARG_NAME}
PRIVATE
${PROJECT_SOURCE_DIR}/include
${CMAKE_BINARY_DIR}/include
${PROJECT_BINARY_DIR}/include
)

add_custom_target(run-thread-safety-test-${THREAD_SAFETY_TEST_ARG_NAME}
Expand All @@ -142,7 +142,7 @@ macro(add_thread_safety_test name)
private_add_thread_safety_test(NAME ${name} ${ARGN})
endmacro(add_thread_safety_test name)

set(PERFORMANCE_OUTPUT_DIR "${CMAKE_BINARY_DIR}/performance-output")
set(PERFORMANCE_OUTPUT_DIR "${PROJECT_BINARY_DIR}/performance-output")
file(MAKE_DIRECTORY ${PERFORMANCE_OUTPUT_DIR})

function(private_add_performance_test)
Expand Down Expand Up @@ -183,11 +183,11 @@ function(private_add_performance_test)
target_include_directories(performance-test-${FUNCTION_PERF_ARG_NAME}
PRIVATE
${PROJECT_SOURCE_DIR}/include
${CMAKE_BINARY_DIR}/include
${PROJECT_BINARY_DIR}/include
)

add_custom_target(run-performance-test-${FUNCTION_PERF_ARG_NAME}
COMMAND ${CMAKE_BINARY_DIR}/performance-test-${FUNCTION_PERF_ARG_NAME} --benchmark_out=${PERFORMANCE_OUTPUT_DIR}/${FUNCTION_PERF_ARG_NAME}.json --benchmark_out_format=json
COMMAND ${PROJECT_BINARY_DIR}/performance-test-${FUNCTION_PERF_ARG_NAME} --benchmark_out=${PERFORMANCE_OUTPUT_DIR}/${FUNCTION_PERF_ARG_NAME}.json --benchmark_out_format=json
DEPENDS performance-test-${FUNCTION_PERF_ARG_NAME}
)
endfunction(private_add_performance_test)
Expand Down Expand Up @@ -229,11 +229,11 @@ function(private_add_single_file_performance_test)
target_include_directories(performance-test-single-file-${FUNCTION_PERF_ARG_NAME}
PRIVATE
${PROJECT_SOURCE_DIR}/include
${CMAKE_BINARY_DIR}/include
${PROJECT_BINARY_DIR}/include
)

add_custom_target(run-performance-test-single-file-${FUNCTION_PERF_ARG_NAME}
COMMAND ${CMAKE_BINARY_DIR}/performance-test-single-file-${FUNCTION_PERF_ARG_NAME} --benchmark_out=${PERFORMANCE_OUTPUT_DIR}/${FUNCTION_PERF_ARG_NAME}.json --benchmark_out_format=json
COMMAND ${PROJECT_BINARY_DIR}/performance-test-single-file-${FUNCTION_PERF_ARG_NAME} --benchmark_out=${PERFORMANCE_OUTPUT_DIR}/${FUNCTION_PERF_ARG_NAME}.json --benchmark_out_format=json
DEPENDS performance-test-single-file-${FUNCTION_PERF_ARG_NAME}
)
endfunction(private_add_single_file_performance_test)
Expand Down Expand Up @@ -275,13 +275,13 @@ function(private_add_fuzz_test)
target_include_directories(fuzz-test-${FUNCTION_FUZZ_ARG_NAME}
PRIVATE
${PROJECT_SOURCE_DIR}/include
${CMAKE_BINARY_DIR}/include
${PROJECT_BINARY_DIR}/include
)

set(generated_corpus_dir ${CMAKE_CURRENT_BINARY_DIR}/fuzz-corpora/${FUNCTION_FUZZ_ARG_CORPUS_NAME})
set(generated_corpus_dir ${PROJECT_BINARY_DIR}/fuzz-corpora/${FUNCTION_FUZZ_ARG_CORPUS_NAME})
file(MAKE_DIRECTORY ${generated_corpus_dir})
add_custom_target(run-fuzz-test-${FUNCTION_FUZZ_ARG_NAME}
COMMAND ${CMAKE_BINARY_DIR}/fuzz-test-${FUNCTION_FUZZ_ARG_NAME} ${generated_corpus_dir} "${FUZZ_CORPORA_DIR}/${FUNCTION_FUZZ_ARG_CORPUS_NAME}"
COMMAND ${PROJECT_BINARY_DIR}/fuzz-test-${FUNCTION_FUZZ_ARG_NAME} ${generated_corpus_dir} "${FUZZ_CORPORA_DIR}/${FUNCTION_FUZZ_ARG_CORPUS_NAME}"
DEPENDS fuzz-test-${FUNCTION_FUZZ_ARG_NAME}
)
endfunction(private_add_fuzz_test)
Expand All @@ -308,7 +308,7 @@ add_dependencies(test_helper_fixture libgtest)
target_include_directories(test_helper_fixture
PRIVATE
${PROJECT_SOURCE_DIR}/include
${CMAKE_BINARY_DIR}/include
${PROJECT_BINARY_DIR}/include
)

add_library(test_helper_network
Expand All @@ -326,7 +326,7 @@ add_dependencies(test_helper_network libgtest)
target_include_directories(test_helper_network
PRIVATE
${PROJECT_SOURCE_DIR}/include
${CMAKE_BINARY_DIR}/include
${PROJECT_BINARY_DIR}/include
)

add_library(test_helper_resolve
Expand All @@ -342,7 +342,7 @@ set_target_properties(test_helper_resolve
target_include_directories(test_helper_resolve
PRIVATE
${PROJECT_SOURCE_DIR}/include
${CMAKE_BINARY_DIR}/include
${PROJECT_BINARY_DIR}/include
)
add_library(test_helper_rfc5424
EXCLUDE_FROM_ALL
Expand All @@ -359,7 +359,7 @@ add_dependencies(test_helper_rfc5424 libgtest)
target_include_directories(test_helper_rfc5424
PRIVATE
${PROJECT_SOURCE_DIR}/include
${CMAKE_BINARY_DIR}/include
${PROJECT_BINARY_DIR}/include
)

add_library(test_helper_server
Expand All @@ -375,7 +375,7 @@ set_target_properties(test_helper_server
target_include_directories(test_helper_server
PRIVATE
${PROJECT_SOURCE_DIR}/include
${CMAKE_BINARY_DIR}/include
${PROJECT_BINARY_DIR}/include
)

add_library(test_helper_usage
Expand All @@ -391,5 +391,5 @@ set_target_properties(test_helper_usage
target_include_directories(test_helper_usage
PRIVATE
${PROJECT_SOURCE_DIR}/include
${CMAKE_BINARY_DIR}/include
${PROJECT_BINARY_DIR}/include
)

0 comments on commit 279d7cb

Please sign in to comment.