Allow lexers to build into kicommon

This commit is contained in:
Marek Roszko 2024-01-04 23:32:06 -05:00
parent 8d9185919a
commit 23f35e1c8b
3 changed files with 65 additions and 26 deletions

View File

@ -56,6 +56,10 @@
# *.h lexfer file. If not defined, the output path is the same # *.h lexfer file. If not defined, the output path is the same
# path as the token list file path, with a file name of *_lexer.h # path as the token list file path, with a file name of *_lexer.h
# #
# exportMacro - Optional, the name of the macro used for dllexport/dllimport and is used
# to mark the class for export
# exportMacroInclude - Optional, a include that is added for use of the export macro
#
# Use the max_lexer() CMake function from functions.cmake for invocation convenience. # Use the max_lexer() CMake function from functions.cmake for invocation convenience.
@ -92,6 +96,10 @@ if( NOT DEFINED outHeaderFile )
set( outHeaderFile "${outputPath}/${result}_lexer.h" ) set( outHeaderFile "${outputPath}/${result}_lexer.h" )
endif() endif()
if( exportMacro )
set( exportMacro "${exportMacro} ")
endif()
# Create tag for generating header file. # Create tag for generating header file.
set( headerTag "${LEXERCLASS}_H_" ) set( headerTag "${LEXERCLASS}_H_" )
@ -105,7 +113,18 @@ set( includeFileHeader
#define ${headerTag} #define ${headerTag}
#include <dsnlexer.h> #include <dsnlexer.h>
")
if( exportMacroInclude )
set( includeFileHeader
"${includeFileHeader}
#include <${exportMacroInclude}>
"
)
endif()
set( includeFileHeader
"${includeFileHeader}
/** /**
* C++ does not put enum _values_ in separate namespaces unless the enum itself * C++ does not put enum _values_ in separate namespaces unless the enum itself
* is in a separate namespace. All the token enums must be in separate namespaces * is in a separate namespace. All the token enums must be in separate namespaces
@ -234,7 +253,7 @@ file( APPEND "${outHeaderFile}"
* technology, based on keywords provided by file: * technology, based on keywords provided by file:
* ${inputFile} * ${inputFile}
*/ */
class ${LEXERCLASS} : public DSNLEXER class ${exportMacro}${LEXERCLASS} : public DSNLEXER
{ {
/// Auto generated lexer keywords table and length: /// Auto generated lexer keywords table and length:
static const KEYWORD keywords[]; static const KEYWORD keywords[];
@ -397,7 +416,7 @@ const char* ${LEXERCLASS}::TokenName( T aTok )
) )
file( APPEND "${outCppFile}" file( APPEND "${outCppFile}"
" "
const KEYWORD_MAP ${LEXERCLASS}::keywords_hash({ const KEYWORD_MAP ${LEXERCLASS}::keywords_hash({
@ -410,7 +429,7 @@ math( EXPR tokensAfter "${tokensAfter} - 1" )
foreach( token ${tokens} ) foreach( token ${tokens} )
file(APPEND "${outCppFile}" " { \"${token}\", ${TOKEN_NUM} }" ) file(APPEND "${outCppFile}" " { \"${token}\", ${TOKEN_NUM} }" )
if( TOKEN_NUM EQUAL tokensAfter ) if( TOKEN_NUM EQUAL tokensAfter )
file( APPEND "${outCppFile}" "\n" ) file( APPEND "${outCppFile}" "\n" )
else( TOKEN_NUM EQUAL tokensAfter ) else( TOKEN_NUM EQUAL tokensAfter )

View File

@ -19,19 +19,12 @@
# or you may search the http://www.gnu.org website for the version 2 license, # or you may search the http://www.gnu.org website for the version 2 license,
# or you may write to the Free Software Foundation, Inc., # or you may write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#
# Function make_lexer # make_lexer_export
# is a standard way to invoke TokenList2DsnLexer.cmake. # This function performs the same job as make_lexer but with two additional parameters to specify
# Extra arguments are treated as source files which depend on the generated # the export macro
# files. Some detail here on the indirection: function( make_lexer_export outputTarget inputFile outHeaderFile outCppFile enum exportMacro exportMacroInclude )
# - Parallel builds all depend on the same files, and CMake will generate the same file multiple times in the same location.
# This can be problematic if the files are generated at the same time and overwrite each other.
# - To fix this, we create a custom target (outputTarget) that the parallel builds depend on.
# AND build dependencies. This creates the needed rebuild for appropriate source object changes.
function( make_lexer outputTarget inputFile outHeaderFile outCppFile enum )
add_custom_command( add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${outHeaderFile} OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${outHeaderFile}
${CMAKE_CURRENT_BINARY_DIR}/${outCppFile} ${CMAKE_CURRENT_BINARY_DIR}/${outCppFile}
@ -40,6 +33,8 @@ function( make_lexer outputTarget inputFile outHeaderFile outCppFile enum )
-DinputFile=${CMAKE_CURRENT_SOURCE_DIR}/${inputFile} -DinputFile=${CMAKE_CURRENT_SOURCE_DIR}/${inputFile}
-DoutHeaderFile=${CMAKE_CURRENT_BINARY_DIR}/${outHeaderFile} -DoutHeaderFile=${CMAKE_CURRENT_BINARY_DIR}/${outHeaderFile}
-DoutCppFile=${CMAKE_CURRENT_BINARY_DIR}/${outCppFile} -DoutCppFile=${CMAKE_CURRENT_BINARY_DIR}/${outCppFile}
-DexportMacro=${exportMacro}
-DexportMacroInclude=${exportMacroInclude}
-P ${KICAD_CMAKE_MODULE_PATH}/BuildSteps/TokenList2DsnLexer.cmake -P ${KICAD_CMAKE_MODULE_PATH}/BuildSteps/TokenList2DsnLexer.cmake
COMMENT "TokenList2DsnLexer.cmake creating: COMMENT "TokenList2DsnLexer.cmake creating:
${outHeaderFile} and ${outHeaderFile} and
@ -54,6 +49,19 @@ function( make_lexer outputTarget inputFile outHeaderFile outCppFile enum )
endfunction() endfunction()
# Function make_lexer
# is a standard way to invoke TokenList2DsnLexer.cmake.
# Extra arguments are treated as source files which depend on the generated
# files. Some detail here on the indirection:
# - Parallel builds all depend on the same files, and CMake will generate the same file multiple times in the same location.
# This can be problematic if the files are generated at the same time and overwrite each other.
# - To fix this, we create a custom target (outputTarget) that the parallel builds depend on.
# AND build dependencies. This creates the needed rebuild for appropriate source object changes.
function( make_lexer outputTarget inputFile outHeaderFile outCppFile enum )
make_lexer_export( ${outputTarget} ${inputFile} ${outHeaderFile} ${outCppFile} ${enum} "" "" )
endfunction()
# Function generate_lemon_grammar # Function generate_lemon_grammar
# #
# This is a function to create a custom command to generate a parser grammar using lemon. # This is a function to create a custom command to generate a parser grammar using lemon.

View File

@ -781,21 +781,25 @@ generate_lemon_grammar(
# Called twice one for common and one for gal, to ensure the files are created # Called twice one for common and one for gal, to ensure the files are created
# on all devel tools ( Linux and msys2 ) # on all devel tools ( Linux and msys2 )
# works on Linux: # works on Linux:
make_lexer( make_lexer_export(
common kicommon
stroke_params.keywords stroke_params.keywords
stroke_params_lexer.h stroke_params_lexer.h
stroke_params_keywords.cpp stroke_params_keywords.cpp
STROKEPARAMS_T STROKEPARAMS_T
KICOMMON_API
kicommon.h
) )
# auto-generate netlist_lexer.h and netlist_keywords.cpp # auto-generate netlist_lexer.h and netlist_keywords.cpp
make_lexer( make_lexer_export(
common kicommon
netlist.keywords netlist.keywords
netlist_lexer.h netlist_lexer.h
netlist_keywords.cpp netlist_keywords.cpp
NL_T NL_T
KICOMMON_API
kicommon.h
) )
# auto-generate pcb_plot_params_lexer.h and pcb_plot_params_keywords.cpp # auto-generate pcb_plot_params_lexer.h and pcb_plot_params_keywords.cpp
@ -808,12 +812,14 @@ make_lexer(
) )
# auto-generate drc_rules_lexer.h and drc_rules_keywords.cpp # auto-generate drc_rules_lexer.h and drc_rules_keywords.cpp
make_lexer( make_lexer_export(
common kicommon
drc_rules.keywords drc_rules.keywords
drc_rules_lexer.h drc_rules_lexer.h
drc_rules_keywords.cpp drc_rules_keywords.cpp
DRCRULE_T DRCRULE_T
KICOMMON_API
kicommon.h
) )
@ -827,31 +833,37 @@ make_lexer(
) )
# auto-generate s-expression library table code. # auto-generate s-expression library table code.
make_lexer( make_lexer_export(
common kicommon
lib_table.keywords lib_table.keywords
lib_table_lexer.h lib_table_lexer.h
lib_table_keywords.cpp lib_table_keywords.cpp
LIB_TABLE_T LIB_TABLE_T
KICOMMON_API
kicommon.h
) )
# auto-generate s-expression template fieldnames lexer and keywords. # auto-generate s-expression template fieldnames lexer and keywords.
make_lexer( make_lexer_export(
common kicommon
template_fieldnames.keywords template_fieldnames.keywords
template_fieldnames_lexer.h template_fieldnames_lexer.h
template_fieldnames_keywords.cpp template_fieldnames_keywords.cpp
TFIELD_T TFIELD_T
KICOMMON_API
kicommon.h
) )
# auto-generate page layout reader s-expression page_layout_reader_lexer.h # auto-generate page layout reader s-expression page_layout_reader_lexer.h
# and title_block_reader_keywords.cpp. # and title_block_reader_keywords.cpp.
make_lexer( make_lexer_export(
common kicommon
drawing_sheet/drawing_sheet.keywords drawing_sheet/drawing_sheet.keywords
drawing_sheet/drawing_sheet_lexer.h drawing_sheet/drawing_sheet_lexer.h
drawing_sheet/drawing_sheet_keywords.cpp drawing_sheet/drawing_sheet_keywords.cpp
DRAWINGSHEET_T DRAWINGSHEET_T
KICOMMON_API
kicommon.h
) )
# This one gets made only when testing. # This one gets made only when testing.