|
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 |
3 | 3 |
|
4 | 4 | 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}") |
13 | 17 |
|
14 | 18 | elseif(STEP STREQUAL "pp2") |
15 | | - # Step 2: gasp/masp preprocessor |
| 19 | + # Step 2: gasp/masp preprocessor for macro expansion |
16 | 20 | if(NOT DEFINED GASP_TOOL) |
17 | 21 | message(FATAL_ERROR "GASP_TOOL not defined") |
18 | 22 | endif() |
19 | | - # Use wrapper script for better error handling |
| 23 | + |
| 24 | + # Run masp directly |
20 | 25 | 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}" |
22 | 27 | RESULT_VARIABLE result |
23 | 28 | OUTPUT_VARIABLE output |
24 | 29 | ERROR_VARIABLE error |
25 | 30 | ) |
26 | 31 | 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}") |
28 | 36 | endif() |
29 | 37 |
|
30 | 38 | elseif(STEP STREQUAL "pp3") |
31 | 39 | # 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}") |
39 | 46 |
|
40 | 47 | 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 |
45 | 57 | 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}" |
47 | 59 | RESULT_VARIABLE result |
| 60 | + OUTPUT_VARIABLE output |
| 61 | + ERROR_VARIABLE error |
48 | 62 | ) |
49 | 63 | if(NOT result EQUAL 0) |
50 | | - message(FATAL_ERROR "Step 4 preprocessing failed") |
| 64 | + message(FATAL_ERROR "C preprocessor failed (exit ${result})\nError: ${error}") |
51 | 65 | endif() |
| 66 | + file(WRITE "${OUTPUT}" "${output}") |
52 | 67 |
|
53 | 68 | else() |
54 | 69 | message(FATAL_ERROR "Unknown step: ${STEP}") |
|
0 commit comments