cmake: use repo-relative paths for flex/bison #line output
Invoke flex and bison from CMAKE_SOURCE_DIR with relative input and output paths so generated parser/lexer sources do not embed absolute build directory paths. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+53
-36
@@ -1,6 +1,13 @@
|
|||||||
# Generic Flex/Yacc Generation Functions
|
# Generic Flex/Yacc Generation Functions
|
||||||
# This file provides reusable functions for generating C++ files from Flex/Bison sources
|
# This file provides reusable functions for generating C++ files from Flex/Bison sources
|
||||||
|
|
||||||
|
# Emit #line paths relative to CMAKE_SOURCE_DIR so generated sources do not embed
|
||||||
|
# absolute build-tree paths (helps reproducible builds and Yocto QA buildpaths).
|
||||||
|
function(_flexyacc_path_relative_to_source OUT_VAR ABS_PATH)
|
||||||
|
file(RELATIVE_PATH ${OUT_VAR} ${CMAKE_SOURCE_DIR} ${ABS_PATH})
|
||||||
|
set(${OUT_VAR} ${${OUT_VAR}} PARENT_SCOPE)
|
||||||
|
endfunction()
|
||||||
|
|
||||||
# Function to generate Flex lexer files
|
# Function to generate Flex lexer files
|
||||||
# Usage: generate_flex_lexer(OUTPUT_VAR INPUT_FILE [PREFIX] [HEADER_DEPENDENCY])
|
# Usage: generate_flex_lexer(OUTPUT_VAR INPUT_FILE [PREFIX] [HEADER_DEPENDENCY])
|
||||||
# OUTPUT_VAR: Variable name to store the output file path
|
# OUTPUT_VAR: Variable name to store the output file path
|
||||||
@@ -8,31 +15,36 @@
|
|||||||
# PREFIX: Optional prefix for the generated files (defaults to basename of input file)
|
# PREFIX: Optional prefix for the generated files (defaults to basename of input file)
|
||||||
# HEADER_DEPENDENCY: Optional header file that the lexer depends on (e.g., from Bison)
|
# HEADER_DEPENDENCY: Optional header file that the lexer depends on (e.g., from Bison)
|
||||||
function(generate_flex_lexer OUTPUT_VAR INPUT_FILE)
|
function(generate_flex_lexer OUTPUT_VAR INPUT_FILE)
|
||||||
get_filename_component(INPUT_BASENAME ${INPUT_FILE} NAME_WE)
|
get_filename_component(INPUT_BASENAME ${INPUT_FILE} NAME_WE)
|
||||||
|
|
||||||
if(ARGC GREATER 2)
|
if(ARGC GREATER 2)
|
||||||
set(PREFIX ${ARGV2})
|
set(PREFIX ${ARGV2})
|
||||||
else()
|
else()
|
||||||
set(PREFIX ${INPUT_BASENAME})
|
set(PREFIX ${INPUT_BASENAME})
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
set(LEX_OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}.cc)
|
set(LEX_OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}.cc)
|
||||||
set(LEX_HEADER ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}.hh)
|
set(LEX_HEADER ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}.hh)
|
||||||
|
|
||||||
# Set up dependencies
|
_flexyacc_path_relative_to_source(LEX_INPUT ${INPUT_FILE})
|
||||||
set(DEPENDENCIES ${INPUT_FILE})
|
_flexyacc_path_relative_to_source(LEX_OUTPUT_REL ${LEX_OUTPUT})
|
||||||
if(ARGC GREATER 3)
|
_flexyacc_path_relative_to_source(LEX_HEADER_REL ${LEX_HEADER})
|
||||||
list(APPEND DEPENDENCIES ${ARGV3})
|
|
||||||
endif()
|
|
||||||
|
|
||||||
add_custom_command(
|
# Set up dependencies
|
||||||
OUTPUT ${LEX_OUTPUT}
|
set(DEPENDENCIES ${INPUT_FILE})
|
||||||
DEPENDS ${DEPENDENCIES}
|
if(ARGC GREATER 3)
|
||||||
COMMAND ${FLEX_EXECUTABLE} --header-file=${LEX_HEADER} -o ${LEX_OUTPUT} ${INPUT_FILE}
|
list(APPEND DEPENDENCIES ${ARGV3})
|
||||||
COMMENT "Generating ${PREFIX}.cc from ${INPUT_FILE}"
|
endif()
|
||||||
)
|
|
||||||
|
|
||||||
set(${OUTPUT_VAR} ${LEX_OUTPUT} PARENT_SCOPE)
|
add_custom_command(
|
||||||
|
OUTPUT ${LEX_OUTPUT}
|
||||||
|
DEPENDS ${DEPENDENCIES}
|
||||||
|
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
|
||||||
|
COMMAND ${FLEX_EXECUTABLE} --header-file=${LEX_HEADER_REL} -o ${LEX_OUTPUT_REL} ${LEX_INPUT}
|
||||||
|
COMMENT "Generating ${PREFIX}.cc from ${INPUT_FILE}"
|
||||||
|
)
|
||||||
|
|
||||||
|
set(${OUTPUT_VAR} ${LEX_OUTPUT} PARENT_SCOPE)
|
||||||
endfunction()
|
endfunction()
|
||||||
|
|
||||||
# Function to generate Bison parser files
|
# Function to generate Bison parser files
|
||||||
@@ -42,26 +54,31 @@ endfunction()
|
|||||||
# INPUT_FILE: Path to the .yy input file
|
# INPUT_FILE: Path to the .yy input file
|
||||||
# PREFIX: Optional prefix for the generated files (defaults to basename of input file)
|
# PREFIX: Optional prefix for the generated files (defaults to basename of input file)
|
||||||
function(generate_bison_parser OUTPUT_VAR HEADER_VAR INPUT_FILE)
|
function(generate_bison_parser OUTPUT_VAR HEADER_VAR INPUT_FILE)
|
||||||
get_filename_component(INPUT_BASENAME ${INPUT_FILE} NAME_WE)
|
get_filename_component(INPUT_BASENAME ${INPUT_FILE} NAME_WE)
|
||||||
|
|
||||||
if(ARGC GREATER 3)
|
if(ARGC GREATER 3)
|
||||||
set(PREFIX ${ARGV3})
|
set(PREFIX ${ARGV3})
|
||||||
else()
|
else()
|
||||||
set(PREFIX ${INPUT_BASENAME})
|
set(PREFIX ${INPUT_BASENAME})
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
set(YACC_OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}.cc)
|
set(YACC_OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}.cc)
|
||||||
set(YACC_HEADER ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}.hh)
|
set(YACC_HEADER ${CMAKE_CURRENT_BINARY_DIR}/${PREFIX}.hh)
|
||||||
|
|
||||||
add_custom_command(
|
_flexyacc_path_relative_to_source(YACC_INPUT ${INPUT_FILE})
|
||||||
OUTPUT ${YACC_OUTPUT} ${YACC_HEADER}
|
_flexyacc_path_relative_to_source(YACC_OUTPUT_REL ${YACC_OUTPUT})
|
||||||
DEPENDS ${INPUT_FILE}
|
_flexyacc_path_relative_to_source(YACC_HEADER_REL ${YACC_HEADER})
|
||||||
COMMAND ${BISON_EXECUTABLE} -p ${PREFIX} --header=${YACC_HEADER} -o ${YACC_OUTPUT} ${INPUT_FILE}
|
|
||||||
COMMENT "Generating ${PREFIX}.cc and ${PREFIX}.hh from ${INPUT_FILE}"
|
|
||||||
)
|
|
||||||
|
|
||||||
set(${OUTPUT_VAR} ${YACC_OUTPUT} PARENT_SCOPE)
|
add_custom_command(
|
||||||
set(${HEADER_VAR} ${YACC_HEADER} PARENT_SCOPE)
|
OUTPUT ${YACC_OUTPUT} ${YACC_HEADER}
|
||||||
|
DEPENDS ${INPUT_FILE}
|
||||||
|
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
|
||||||
|
COMMAND ${BISON_EXECUTABLE} -p ${PREFIX} --header=${YACC_HEADER_REL} -o ${YACC_OUTPUT_REL} ${YACC_INPUT}
|
||||||
|
COMMENT "Generating ${PREFIX}.cc and ${PREFIX}.hh from ${INPUT_FILE}"
|
||||||
|
)
|
||||||
|
|
||||||
|
set(${OUTPUT_VAR} ${YACC_OUTPUT} PARENT_SCOPE)
|
||||||
|
set(${HEADER_VAR} ${YACC_HEADER} PARENT_SCOPE)
|
||||||
endfunction()
|
endfunction()
|
||||||
|
|
||||||
# Generate device attachment parser files using the generic functions
|
# Generate device attachment parser files using the generic functions
|
||||||
|
|||||||
Reference in New Issue
Block a user