Skip to content

Commit 88ee3da

Browse files
committed
Update preprocess process
1 parent 51b5ef4 commit 88ee3da

File tree

1 file changed

+42
-27
lines changed

1 file changed

+42
-27
lines changed

cmake/preprocess_vu1.cmake

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,69 @@
1-
# VU1 preprocessing script
2-
# Usage: cmake -D INPUT=<input> -D OUTPUT=<output> -D STEP=<step> -D SOURCE_DIR=<dir> -D COMPILER=<cc> -D MEM_HEADER=<header> -P preprocess_vu1.cmake
1+
# VU1 preprocessing script - Simplified version
2+
# Usage: cmake -D INPUT=<input> -D OUTPUT=<output> -D STEP=<step> -D SOURCE_DIR=<dir> -D COMPILER=<cc> -D MEM_HEADER=<header> -D GASP_TOOL=<masp> -P preprocess_vu1.cmake
33

44
if(STEP STREQUAL "pp1")
5-
# Step 1: Remove #include, #define, fix .include paths
6-
execute_process(
7-
COMMAND /bin/bash -c "cat ${INPUT} | sed -E 's/#include[[:space:]]+.+// ; s/#define[[:space:]]+.+// ; s|(\\.include[[:space:]]+)\\\"([^/].+)\\\"|\\1\\\"${SOURCE_DIR}/vu1/\\2\\\"|' > ${OUTPUT}"
8-
RESULT_VARIABLE result
9-
)
10-
if(NOT result EQUAL 0)
11-
message(FATAL_ERROR "Step 1 preprocessing failed")
12-
endif()
5+
# Step 1: Clean up C preprocessor directives and fix .include paths
6+
# - Remove #include and #define (will use gasp-style includes and C preprocessor later)
7+
# - Fix .include paths to be absolute
8+
file(READ "${INPUT}" content)
9+
# Remove #include lines
10+
string(REGEX REPLACE "#include[^\n]*\n" "" content "${content}")
11+
# Remove #define lines
12+
string(REGEX REPLACE "#define[^\n]*\n" "" content "${content}")
13+
# Fix .include paths to be absolute (only for relative paths)
14+
# Note: CMake regex doesn't support [[:space:]], use [ \t] instead
15+
string(REGEX REPLACE "\\.include[ \t]+\"([^/][^\"]*)\"" ".include \"${SOURCE_DIR}/vu1/\\1\"" content "${content}")
16+
file(WRITE "${OUTPUT}" "${content}")
1317

1418
elseif(STEP STREQUAL "pp2")
15-
# Step 2: gasp/masp preprocessor
19+
# Step 2: gasp/masp preprocessor for macro expansion
1620
if(NOT DEFINED GASP_TOOL)
1721
message(FATAL_ERROR "GASP_TOOL not defined")
1822
endif()
19-
# Use wrapper script for better error handling
23+
24+
# Run masp directly
2025
execute_process(
21-
COMMAND ${SOURCE_DIR}/cmake/run_masp.sh ${GASP_TOOL} ${SOURCE_DIR}/vu1 ${OUTPUT} ${INPUT}
26+
COMMAND "${GASP_TOOL}" -c ";" -I"${SOURCE_DIR}/vu1" -o "${OUTPUT}" "${INPUT}"
2227
RESULT_VARIABLE result
2328
OUTPUT_VARIABLE output
2429
ERROR_VARIABLE error
2530
)
2631
if(NOT result EQUAL 0)
27-
message(FATAL_ERROR "Step 2 preprocessing (${GASP_TOOL}) failed\nOutput: ${output}\nError: ${error}\nInput: ${INPUT}\nOutput: ${OUTPUT}")
32+
message(FATAL_ERROR "masp failed (exit ${result})\nCommand: ${GASP_TOOL} -c \";\" -I${SOURCE_DIR}/vu1 -o ${OUTPUT} ${INPUT}\nOutput: ${output}\nError: ${error}")
33+
endif()
34+
if(NOT EXISTS "${OUTPUT}")
35+
message(FATAL_ERROR "masp did not create output file: ${OUTPUT}")
2836
endif()
2937

3038
elseif(STEP STREQUAL "pp3")
3139
# Step 3: Array notation conversion
32-
execute_process(
33-
COMMAND /bin/bash -c "cat ${INPUT} | sed -E 's/\\[([0-9])\\]/_\\1/g ; s/\\[([w-zW-Z])\\]/\\1/g' > ${OUTPUT}"
34-
RESULT_VARIABLE result
35-
)
36-
if(NOT result EQUAL 0)
37-
message(FATAL_ERROR "Step 3 preprocessing failed")
38-
endif()
40+
# Convert [0] -> _0, [1] -> _1, etc.
41+
# Convert [x] -> x, [y] -> y, etc. (vector component access)
42+
file(READ "${INPUT}" content)
43+
string(REGEX REPLACE "\\[([0-9])\\]" "_\\1" content "${content}")
44+
string(REGEX REPLACE "\\[([w-zW-Z])\\]" "\\1" content "${content}")
45+
file(WRITE "${OUTPUT}" "${content}")
3946

4047
elseif(STEP STREQUAL "pp4")
41-
# Step 4: C preprocessor with memory layout
42-
# Use -w to suppress warnings about unmatched quotes in assembly comments
43-
# Escape backslashes before preprocessing, then restore them after
44-
# This preserves masp/gasp local labels like \xformed_vert while allowing normal C preprocessing
48+
# Step 4: C preprocessor for memory layout evaluation
49+
if(NOT DEFINED COMPILER)
50+
message(FATAL_ERROR "COMPILER not defined")
51+
endif()
52+
if(NOT DEFINED MEM_HEADER)
53+
message(FATAL_ERROR "MEM_HEADER not defined")
54+
endif()
55+
56+
# Use -x assembler-with-cpp to force GCC to preprocess .vcl files as assembly
4557
execute_process(
46-
COMMAND /bin/bash -c "sed 's/\\\\/\\\\\\\\/g' ${INPUT} | ${COMPILER} -E -P -w -I${SOURCE_DIR}/vu1 -imacros ${MEM_HEADER} - | sed 's/\\\\\\\\/\\\\/g' > ${OUTPUT}"
58+
COMMAND "${COMPILER}" -E -P -w -x assembler-with-cpp -I"${SOURCE_DIR}/vu1" -imacros "${MEM_HEADER}" "${INPUT}"
4759
RESULT_VARIABLE result
60+
OUTPUT_VARIABLE output
61+
ERROR_VARIABLE error
4862
)
4963
if(NOT result EQUAL 0)
50-
message(FATAL_ERROR "Step 4 preprocessing failed")
64+
message(FATAL_ERROR "C preprocessor failed (exit ${result})\nError: ${error}")
5165
endif()
66+
file(WRITE "${OUTPUT}" "${output}")
5267

5368
else()
5469
message(FATAL_ERROR "Unknown step: ${STEP}")

0 commit comments

Comments
 (0)