Skip to content

Commit 214be74

Browse files
committed
[cmake/generate_header_and_source_from_header_only.cmake] Work on new handwritten implementation (ughhh)
1 parent 7c33675 commit 214be74

File tree

10 files changed

+722
-263
lines changed

10 files changed

+722
-263
lines changed

acquire/CMakeLists.txt

Lines changed: 30 additions & 240 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ set_extract_lib()
4141
include("${CMAKE_SOURCE_DIR}/cmake/FindLibAcquire.cmake")
4242

4343
foreach (_library "HTTPS" "CRYPTO" "EXTRACT" "CHECKSUM")
44-
message(STATUS "[libacquire] ${_library}_LIB:\t${${_library}_LIB}")
44+
message(STATUS "[libacquire] ${_library}_LIB:\t\t${${_library}_LIB}")
4545
endforeach (_library "HTTPS" "CRYPTO" "EXTRACT" "CHECKSUM")
4646

4747
set_checksum_lib()
@@ -68,241 +68,8 @@ source_group("Header Files" FILES "${Header_Files}")
6868

6969
option(DEBUG_TOKENISER "OFF")
7070

71-
function(generate_source_file_from_header_only filepath header_filepath source_filepath)
72-
if (NOT EXISTS "${filepath}")
73-
message(FATAL_ERROR "Input file does not exist: ${filepath}")
74-
return()
75-
endif (NOT EXISTS "${filepath}")
76-
77-
get_filename_component(header_basename "${header_filepath}" NAME)
78-
set(header_contents "")
79-
set(source_contents "")
80-
81-
# Parsing State
82-
set(parsing_mode "HEADER") # HEADER or SOURCE (based on LIBACQUIRE_IMPLEMENTATION block)
83-
set(current_line "") # Accumulates characters for the current logical line/directive
84-
set(state "CODE") # Current lexical state: CODE, BLOCK_COMMENT, LINE_COMMENT, STRING, CHAR_LITERAL
85-
set(escape_next_char OFF) # Flag for handling backslash escapes
86-
87-
# Parenthesis/Brace/Bracket Balancing (within CODE state)
88-
set(lparen 0)
89-
set(rparen 0)
90-
set(lbrace 0) # Not used for line ending detection, but might be useful
91-
set(rbrace 0) # Not used for line ending detection
92-
set(lsquare 0)
93-
set(rsquare 0)
94-
95-
# Preprocessor Condition Stack
96-
# Each element is "TYPE;CONDITION_STRING" e.g., "IF;FEATURE_A" or "IFDEF;FOO"
97-
set(pp_conditions_stack "")
98-
# Tracks if we are inside the target implementation block
99-
set(in_implementation_block OFF)
100-
set(implementation_block_depth 0) # Depth specifically for LIBACQUIRE_IMPLEMENTATION blocks
101-
102-
# --- Character-by-Character Processing ---
103-
file(READ "${filepath}" file_content)
104-
string(LENGTH "${file_content}" file_len)
105-
math(EXPR last_index "${file_len} - 1")
106-
107-
set(prev_char "") # Store previous character for multi-char checks like /*, //
108-
109-
foreach (char_index RANGE ${last_index})
110-
string(SUBSTRING "${file_content}" ${char_index} 1 char)
111-
string(APPEND current_line "${char}")
112-
113-
# --- State Machine ---
114-
if (state STREQUAL "CODE")
115-
if (escape_next_char)
116-
set(escape_next_char OFF)
117-
elseif (char STREQUAL "\\")
118-
set(escape_next_char ON)
119-
elseif (char STREQUAL "/" AND prev_char STREQUAL "/")
120-
set(state "LINE_COMMENT")
121-
elseif (char STREQUAL "*" AND prev_char STREQUAL "/")
122-
set(state "BLOCK_COMMENT")
123-
elseif (char STREQUAL "\"")
124-
set(state "STRING")
125-
set(escape_next_char OFF)
126-
elseif (char STREQUAL "'")
127-
set(state "CHAR_LITERAL")
128-
set(escape_next_char OFF)
129-
else ()
130-
# Balance parens/brackets only in code state
131-
if (char STREQUAL "(")
132-
math(EXPR lparen "${lparen} + 1")
133-
elseif (char STREQUAL ")")
134-
math(EXPR rparen "${rparen} + 1")
135-
elseif (char STREQUAL "[")
136-
math(EXPR lsquare "${lsquare} + 1")
137-
elseif (char STREQUAL "]")
138-
math(EXPR rsquare "${rsquare} + 1")
139-
elseif (char STREQUAL "{")
140-
math(EXPR lbrace "${lbrace} + 1")
141-
elseif (char STREQUAL "}")
142-
math(EXPR rbrace "${rbrace} + 1")
143-
endif ()
144-
endif ()
145-
elseif (state STREQUAL "BLOCK_COMMENT")
146-
if (char STREQUAL "/" AND prev_char STREQUAL "*")
147-
set(state "CODE")
148-
endif (char STREQUAL "/" AND prev_char STREQUAL "*")
149-
elseif (state STREQUAL "LINE_COMMENT")
150-
if (char STREQUAL "\n")
151-
set(state "CODE")
152-
endif (char STREQUAL "\n")
153-
elseif (state STREQUAL "STRING")
154-
if (escape_next_char)
155-
set(escape_next_char OFF)
156-
elseif (char STREQUAL "\\")
157-
set(escape_next_char ON)
158-
elseif (char STREQUAL "\"")
159-
set(state "CODE")
160-
endif ()
161-
elseif (state STREQUAL "CHAR_LITERAL")
162-
if (escape_next_char)
163-
set(escape_next_char OFF)
164-
elseif (char STREQUAL "\\")
165-
set(escape_next_char ON)
166-
elseif (char STREQUAL "'")
167-
set(state "CODE")
168-
endif ()
169-
endif ()
170-
171-
set(prev_char "${char}")
172-
173-
# --- Line Boundary Detection (Highly Fragile) ---
174-
set(process_line OFF)
175-
if (char STREQUAL "\n" AND state STREQUAL "CODE")
176-
if (lparen EQUAL rparen AND lsquare EQUAL rsquare)
177-
string(STRIP "${current_line}" trimmed_line)
178-
string(LENGTH "${trimmed_line}" trimmed_len)
179-
if (trimmed_len GREATER 0)
180-
math(EXPR last_trimmed_idx "${trimmed_len} - 1")
181-
string(SUBSTRING "${trimmed_line}" ${last_trimmed_idx} 1 last_trimmed_char)
182-
string(REGEX MATCH "^[ \t]*#" is_preprocessor "${trimmed_line}")
183-
184-
if (last_trimmed_char STREQUAL ";")
185-
set(process_line ON)
186-
elseif (last_trimmed_char STREQUAL "{")
187-
set(process_line ON)
188-
elseif (last_trimmed_char STREQUAL "}")
189-
set(process_line ON)
190-
elseif (is_preprocessor)
191-
set(process_line ON)
192-
elseif (last_trimmed_char STREQUAL ":")
193-
if (NOT trimmed_line MATCHES "[;={}(]")
194-
set(process_line ON)
195-
endif ()
196-
endif ()
197-
else ()
198-
set(process_line ON)
199-
endif ()
200-
endif ()
201-
endif ()
202-
203-
if (process_line)
204-
string(STRIP "${current_line}" line_for_match)
205-
set(append_to_header OFF)
206-
set(append_to_source OFF)
207-
208-
if (in_implementation_block)
209-
set(append_to_source ON)
210-
else ()
211-
set(append_to_header ON)
212-
endif ()
213-
214-
if (line_for_match MATCHES "^[ \t]*#")
215-
if (line_for_match MATCHES "^[ \t]*#[ \t]*if(n?def)?")
216-
string(REGEX REPLACE "^[ \t]*#[ \t]*if(n?def)?[ \t]+" "" condition_str "${line_for_match}")
217-
set(directive_type "IF")
218-
if (line_for_match MATCHES "^[ \t]*#[ \t]*ifdef")
219-
set(directive_type "IFDEF")
220-
endif (line_for_match MATCHES "^[ \t]*#[ \t]*ifdef")
221-
if (line_for_match MATCHES "^[ \t]*#[ \t]*ifndef")
222-
set(directive_type "IFNDEF")
223-
endif (line_for_match MATCHES "^[ \t]*#[ \t]*ifndef")
224-
225-
list(APPEND pp_conditions_stack "${directive_type};${condition_str}")
226-
227-
if (condition_str MATCHES "LIBACQUIRE_IMPLEMENTATION")
228-
if (implementation_block_depth EQUAL 0)
229-
set(in_implementation_block ON)
230-
set(append_to_header OFF)
231-
set(append_to_source ON)
232-
endif (implementation_block_depth EQUAL 0)
233-
math(EXPR implementation_block_depth "${implementation_block_depth} + 1")
234-
endif (condition_str MATCHES "LIBACQUIRE_IMPLEMENTATION")
235-
236-
elseif (line_for_match MATCHES "^[ \t]*#[ \t]*else")
237-
# pass through
238-
elseif (line_for_match MATCHES "^[ \t]*#[ \t]*elif")
239-
# pass through
240-
elseif (line_for_match MATCHES "^[ \t]*#[ \t]*endif")
241-
if (pp_conditions_stack)
242-
set(is_impl_endif OFF)
243-
if (line_for_match MATCHES "LIBACQUIRE_IMPLEMENTATION")
244-
if (implementation_block_depth GREATER 0)
245-
set(is_impl_endif ON)
246-
endif (implementation_block_depth GREATER 0)
247-
endif (line_for_match MATCHES "LIBACQUIRE_IMPLEMENTATION")
248-
if (is_impl_endif)
249-
math(EXPR implementation_block_depth "${implementation_block_depth} - 1")
250-
if (implementation_block_depth EQUAL 0)
251-
set(in_implementation_block OFF)
252-
set(append_to_header OFF)
253-
set(append_to_source ON)
254-
endif (implementation_block_depth EQUAL 0)
255-
endif (is_impl_endif)
256-
list(POP_BACK pp_conditions_stack)
257-
else ()
258-
message(WARNING "Unmatched #endif encountered: ${current_line}")
259-
endif ()
260-
endif ()
261-
endif (line_for_match MATCHES "^[ \t]*#")
262-
263-
if (append_to_source)
264-
string(APPEND source_contents "${current_line}")
265-
elseif (append_to_header)
266-
string(APPEND header_contents "${current_line}")
267-
endif ()
268-
269-
set(current_line "")
270-
set(lparen 0)
271-
set(rparen 0)
272-
set(lbrace 0)
273-
set(rbrace 0)
274-
set(lsquare 0)
275-
set(rsquare 0)
276-
endif (process_line)
277-
endforeach (char_index RANGE ${last_index})
278-
279-
if (NOT state STREQUAL "CODE")
280-
message(WARNING "File ended while in state: ${state}")
281-
endif (NOT state STREQUAL "CODE")
282-
if (NOT lparen EQUAL rparen OR NOT lsquare EQUAL rsquare)
283-
message(WARNING "File ended with unbalanced parentheses/brackets")
284-
endif (NOT lparen EQUAL rparen OR NOT lsquare EQUAL rsquare)
285-
if (pp_conditions_stack)
286-
message(WARNING "File ended with unbalanced preprocessor directives. Stack: ${pp_conditions_stack}")
287-
endif (pp_conditions_stack)
288-
if (NOT implementation_block_depth EQUAL 0)
289-
message(WARNING "File ended with unbalanced LIBACQUIRE_IMPLEMENTATION blocks. Depth: ${implementation_block_depth}")
290-
endif (NOT implementation_block_depth EQUAL 0)
291-
292-
file(WRITE "${header_filepath}" "${header_contents}")
293-
string(REGEX REPLACE "^${CMAKE_BINARY_DIR}." "" header_filepath_rel "${header_filepath}")
294-
message(STATUS "Generated header (via character-by-character): ${header_filepath_rel}")
295-
296-
string(REGEX REPLACE "^${CMAKE_BINARY_DIR}." "" source_filepath_rel "${source_filepath}")
297-
if (source_contents OR in_implementation_block)
298-
set(final_source_content "#include \"${header_basename}\"\n\n${source_contents}")
299-
file(WRITE "${source_filepath}" "${final_source_content}")
300-
message(STATUS "Generated source (via character-by-character): ${source_filepath_rel}")
301-
else ()
302-
message(STATUS "Failed to source gen (character-by-character): ${source_filepath_rel}")
303-
endif ()
304-
305-
endfunction(generate_source_file_from_header_only filepath header_filepath source_filepath)
71+
include("${CMAKE_SOURCE_DIR}/cmake/generate_header_and_source_from_header_only.cmake")
72+
include("${CMAKE_SOURCE_DIR}/cmake/SplitHeaderOnly.cmake")
30673

30774
if (LIBACQUIRE_HEADER_ONLY)
30875
add_library("${LIBRARY_NAME}" INTERFACE "${_Header_Files}" "${Header_Files}")
@@ -324,16 +91,33 @@ else ()
32491
"acquire_url_utils.h"
32592
)
32693

94+
if (USE_LIBCURL)
95+
list(APPEND header_impls "acquire_libcurl.h")
96+
elseif (USE_LIBFETCH)
97+
list(APPEND header_impls "acquire_libfetch.h")
98+
elseif (USE_WININET)
99+
list(APPEND header_impls "acquire_wininet.h")
100+
elseif (USE_OPENBSD_FTP)
101+
list(APPEND header_impls "acquire_openbsd_ftp.h")
102+
endif ()
103+
104+
327105
foreach (header_file IN LISTS header_impls)
328106
get_filename_component(name_no_ext "${header_file}" NAME_WE)
329107
set(gen_header_file "${CMAKE_BINARY_DIR}/gen/gen_${name_no_ext}.h")
330108
set(gen_source_file "${CMAKE_BINARY_DIR}/gen/gen_${name_no_ext}.c")
331109

332-
generate_source_file_from_header_only(
110+
generate_header_and_source_from_header_only(
333111
"${CMAKE_CURRENT_SOURCE_DIR}/${header_file}"
334112
"${gen_header_file}"
335113
"${gen_source_file}"
336114
)
115+
# split_header_only(
116+
# "${CMAKE_CURRENT_SOURCE_DIR}/${header_file}"
117+
# "${gen_header_file}"
118+
# "${gen_source_file}"
119+
# "LIBACQUIRE_IMPLEMENTATION"
120+
# )
337121

338122
if (EXISTS "${gen_header_file}")
339123
list(APPEND gen_header_files "${gen_header_file}")
@@ -403,14 +187,20 @@ else ()
403187
endif ()
404188
endforeach (src IN LISTS gen_source_files)
405189

406-
if (USE_LIBRHASH)
190+
if (DEFINED CHECKSUM_LIBRARIES)
191+
message(STATUS "[libacquire] Using var ${CHECKSUM_LIBRARIES} for checksums")
192+
target_link_libraries("${LIBRARY_NAME}_impl" PUBLIC "${CHECKSUM_LIBRARIES}")
193+
target_compile_definitions("${LIBRARY_NAME}_impl" PRIVATE "${CHECKSUM_LIBRARIES_USE}=1")
194+
elseif (USE_LIBRHASH)
407195
message(STATUS "[libacquire] Using LibRHash for checksums")
408196

409197
find_package(LibRHash REQUIRED)
410198
target_compile_definitions("${LIBRARY_NAME}_impl" PRIVATE USE_LIBRHASH=1)
411199

412200
get_target_property(linked_libs "${LIBRARY_NAME}_impl" LINK_LIBRARIES)
413-
list(FIND linked_libs "${_lib}" lib_index)
201+
message(STATUS "linked_libs = ${linked_libs}")
202+
message(STATUS "CHECKSUM_LIBRARIES at here is ${CHECKSUM_LIBRARIES}")
203+
list(FIND linked_libs "${LibRHash_LIBRARIES}" lib_index)
414204
if (lib_index EQUAL -1)
415205
target_include_directories("${LIBRARY_NAME}_impl" PUBLIC "${LibRHash_INCLUDE_DIRS}")
416206
list(REMOVE_DUPLICATES LibRHash_LIBRARIES)
@@ -422,7 +212,7 @@ else ()
422212
message(STATUS "[libacquire] Using internal CRC32C implementation")
423213
target_compile_definitions("${LIBRARY_NAME}_impl" PRIVATE USE_CRC32C=1)
424214
else ()
425-
message(FATAL_ERROR "No checksum implementation selected: define USE_LIBRHASH or USE_CRC32C")
215+
message(FATAL_ERROR "No checksum implementation selected: define `USE_LIBRHASH` or `USE_CRC32C`")
426216
endif ()
427217

428218
if (NOT BSD)

acquire/acquire_checksums.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ extern LIBACQUIRE_EXPORT bool (*get_checksum_function(enum Checksum checksum))(
8787
#ifndef LIBACQUIRE_ACQUIRE_CHECKSUMS_IMPL
8888
#define LIBACQUIRE_ACQUIRE_CHECKSUMS_IMPL
8989

90+
/* foo foo foo */
9091
enum Checksum string2checksum(const char *const s) {
9192
if (strncasecmp(s, "CRC32C", 6) == 0)
9293
return LIBACQUIRE_CRC32C;

acquire/acquire_fileutils.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ extern LIBACQUIRE_EXPORT bool exists(const char *path);
6161
* @return `-1` if file doesn't exist otherwise its size
6262
*/
6363
extern LIBACQUIRE_EXPORT off_t filesize(const char *path);
64-
6564
/**
6665
* @brief Get the size of a given path
6766
*

acquire/acquire_librhash.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ extern "C" {
1414
#include <stdbool.h>
1515
#else
1616
#include "acquire_stdbool.h"
17-
#endif
17+
#endif /* __cplusplus */
1818

1919
#include <rhash.h>
2020

acquire/cli/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ target_link_libraries(
5454
PRIVATE
5555
"${PROJECT_NAME}_compiler_flags"
5656
"${LIBRARY_NAME}"
57-
"${PROJECT_NAME}_impl"
5857
"${PROJECT_NAME}"
5958
)
6059

acquire/cli/cli.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ extern "C" {
3535
#define false (!true)
3636
typedef size_t bool;
3737

38-
#endif
38+
#endif /* __cplusplus */
3939

4040
#include <stddef.h>
4141
#include <string.h>

0 commit comments

Comments
 (0)