setup_yacc.cmake revision 0bbfda8a
1# 2# Setup yacc-alike to build the parser for the config file. 3# 4# Similarly to the _lex handler, we always use your yacc to build it if 5# you have one. If you don't we can fallback to a prebuilt one, else 6# die. 7 8# Setup flags, and have an escape to debug the parser, if that's ever 9# useful. 10# 11# Making this a list messes with BISON_TARGET() which requires a string 12# according to the docs (though only cmake 3.4 start complaining about 13# getting a list). A string might be nicer, but we'd really need 14# string(CONCAT) for that, and x-ref in do_install.cmake for notes on 15# that. 16set(YFLAGS -d -b gram) 17if(DO_DEBUGPARSER) 18 list(APPEND YFLAGS -t -v) 19 add_definitions(-DYYEBUG=1) 20 message(STATUS "Enabling config parser debug.") 21endif(DO_DEBUGPARSER) 22 23# Override for forcing use of pregen'd source files 24if(NOT FORCE_PREGEN_FILES) 25 # This only finds bison, not yacc. 26 find_package(BISON) 27 # There doesn't seem to be a standard module for yacc, so hand-code 28 # it. 29 find_program(YACC yacc) 30endif() 31 32if(BISON_FOUND) 33 # What a stupid way to spell 'stringify'... 34 string(REPLACE ";" " " _YFSTR "${YFLAGS}") 35 BISON_TARGET(ctwm_parser gram.y ${CMAKE_CURRENT_BINARY_DIR}/gram.tab.c 36 COMPILE_FLAGS ${_YFSTR}) 37elseif(YACC) 38 # Got yacc(1), use it 39 message(STATUS "Found yacc: ${YACC}") 40 add_custom_command(OUTPUT gram.tab.c gram.tab.h 41 DEPENDS gram.y 42 COMMAND ${YACC} ${YFLAGS} ${CMAKE_CURRENT_SOURCE_DIR}/gram.y 43 COMMENT "Building parser with yacc." 44 ) 45else() 46 # No bison, no yacc. Maybe there are prebuilt files? 47 find_file(GRAM_C gram.tab.c 48 PATHS ${GENSRCDIR} NO_DEFAULT_PATH) 49 find_file(GRAM_H gram.tab.h 50 PATHS ${GENSRCDIR} NO_DEFAULT_PATH) 51 if(GRAM_C AND GRAM_H) 52 # Got prebuilt ones, use 'em 53 message(STATUS "No yacc found, using prebuilt gram.tab.*") 54 add_custom_command(OUTPUT gram.tab.h 55 DEPENDS ${GRAM_H} 56 COMMAND cp ${GRAM_H} . 57 COMMENT "Copying in prebuilt gram.tab.h." 58 ) 59 add_custom_command(OUTPUT gram.tab.c 60 DEPENDS ${GRAM_C} 61 COMMAND cp ${GRAM_C} . 62 COMMENT "Copying in prebuilt gram.tab.c." 63 ) 64 # Also need to explicitly tell cmake; otherwise it knows to 65 # pull in gram.tab.c ('cuz it's in CTWMSRC) but doesn't know 66 # in time to pull in gram.tab.h and so blows up. 67 set_source_files_properties(gram.tab.c OBJECT_DEPENDS gram.tab.h) 68 else() 69 # No bison, no yacc, no prebuilt. Boom. 70 message(FATAL_ERROR "Can't find bison/yacc, and no prebuilt files " 71 "available.") 72 endif(GRAM_C AND GRAM_H) 73endif() 74