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# YFLAGS being a list is the Right(tm) choice here, though it messes with
12# BISON_TARGET() below.
13set(YFLAGS -d -b gram)
14if(DO_DEBUGPARSER)
15	list(APPEND YFLAGS -t -v)
16	add_definitions(-DYYEBUG=1)
17	message(STATUS "Enabling config parser debug.")
18endif(DO_DEBUGPARSER)
19
20# Override for forcing use of pregen'd source files
21if(NOT FORCE_PREGEN_FILES)
22	# This only finds bison, not yacc.
23	find_package(BISON)
24	# There doesn't seem to be a standard module for yacc, so hand-code
25	# it.
26	find_program(YACC yacc)
27endif()
28
29if(BISON_FOUND)
30	# BISON_TARGET requires a string, not a list, for COMPILE_FLAGS.
31	# list(JOIN) would be the proper solution here, but requires cmake
32	# 3.12.
33	if(${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION} GREATER 3.12)
34		list(JOIN YFLAGS " " _YFSTR)
35	else()
36		# So until then, this is our stupid stringify hack...
37		string(REPLACE ";" " " _YFSTR "${YFLAGS}")
38	endif()
39	BISON_TARGET(ctwm_parser gram.y ${CMAKE_CURRENT_BINARY_DIR}/gram.tab.c
40		COMPILE_FLAGS ${_YFSTR})
41elseif(YACC)
42	# Got yacc(1), use it
43	message(STATUS "Found yacc: ${YACC}")
44	add_custom_command(OUTPUT gram.tab.c gram.tab.h
45		DEPENDS gram.y
46		COMMAND ${YACC} ${YFLAGS} ${CMAKE_CURRENT_SOURCE_DIR}/gram.y
47		COMMENT "Building parser with yacc."
48	)
49else()
50	# No bison, no yacc.  Maybe there are prebuilt files?
51	find_file(GRAM_C gram.tab.c
52		PATHS ${GENSRCDIR} NO_DEFAULT_PATH)
53	find_file(GRAM_H gram.tab.h
54		PATHS ${GENSRCDIR} NO_DEFAULT_PATH)
55	if(GRAM_C AND GRAM_H)
56		# Got prebuilt ones, use 'em
57		message(STATUS "No yacc found, using prebuilt gram.tab.*")
58		add_custom_command(OUTPUT gram.tab.h
59			DEPENDS ${GRAM_H}
60			COMMAND cp ${GRAM_H} .
61			COMMENT "Copying in prebuilt gram.tab.h."
62		)
63		add_custom_command(OUTPUT gram.tab.c
64			DEPENDS ${GRAM_C}
65			COMMAND cp ${GRAM_C} .
66			COMMENT "Copying in prebuilt gram.tab.c."
67		)
68		# Also need to explicitly tell cmake; otherwise it knows to
69		# pull in gram.tab.c ('cuz it's in CTWMSRC) but doesn't know
70		# in time to pull in gram.tab.h and so blows up.
71		set_source_files_properties(gram.tab.c OBJECT_DEPENDS gram.tab.h)
72	else()
73		# No bison, no yacc, no prebuilt.  Boom.
74		message(FATAL_ERROR "Can't find bison/yacc, and no prebuilt files "
75			"available.")
76	endif(GRAM_C AND GRAM_H)
77endif()
78