1cmake_minimum_required(VERSION 2.8.12)
2if(POLICY CMP0042)
3	cmake_policy(SET CMP0042 NEW)
4endif()
5if(POLICY CMP0028)
6	cmake_policy(SET CMP0028 NEW)
7endif()
8project(cparser C)
9set(cparser_VERSION "1.22.1")
10
11# Enable C99
12if(CMAKE_VERSION VERSION_LESS "3.1")
13	if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
14		set(CMAKE_C_FLAGS "-std=c99 ${CMAKE_C_FLAGS}")
15	endif()
16else()
17	set(CMAKE_C_STANDARD 99)
18endif()
19
20set(SOURCES
21	src/adt/panic.c
22	src/adt/pset_new.c
23	src/adt/strutil.c
24	src/ast/ast.c
25	src/ast/attribute.c
26	src/ast/constfold.c
27	src/ast/entity.c
28	src/ast/printer.c
29	src/ast/string_hash.c
30	src/ast/string_rep.c
31	src/ast/symbol_table.c
32	src/ast/type.c
33	src/ast/type_hash.c
34	src/ast/types.c
35	src/ast/walk.c
36	src/driver/actions.c
37	src/driver/c_driver.c
38	src/driver/diagnostic.c
39	src/driver/driver.c
40	src/driver/help.c
41	src/driver/options.c
42	src/driver/predefs.c
43	src/driver/target.c
44	src/driver/tempfile.c
45	src/driver/timing.c
46	src/driver/warning.c
47	src/firm/ast2firm.c
48	src/firm/firm_opt.c
49	src/firm/jittest.c
50	src/firm/jump_target.c
51	src/firm/mangle.c
52	src/main.c
53	src/parser/builtins.c
54	src/parser/format_check.c
55	src/parser/input.c
56	src/parser/parser.c
57	src/parser/preprocessor.c
58	src/parser/token.c
59	src/wrappergen/write_compoundsizes.c
60	src/wrappergen/write_fluffy.c
61	src/wrappergen/write_jna.c
62)
63include_directories(src)
64
65add_subdirectory(libfirm)
66# Also include generated header files which are dropped into firm's BINARY_DIR.
67include_directories(libfirm/include ${libfirm_BINARY_DIR}/gen/include/libfirm)
68
69add_executable(cparser ${SOURCES})
70target_link_libraries(cparser firm)
71if(UNIX)
72	target_link_libraries(cparser m)
73endif()
74
75set(DEFAULT_SYSTEM_INCLUDE_DIR /usr/include)
76if(APPLE)
77	execute_process(COMMAND xcrun -show-sdk-path
78		OUTPUT_VARIABLE SDK_PATH
79		OUTPUT_STRIP_TRAILING_WHITESPACE
80	)
81	set(DEFAULT_SYSTEM_INCLUDE_DIR ${SDK_PATH}/usr/include)
82endif()
83set(SYSTEM_INCLUDE_DIR ${DEFAULT_SYSTEM_INCLUDE_DIR} CACHE PATH "system include directory")
84set(LOCAL_INCLUDE_DIR /usr/local CACHE PATH "system local include directory")
85set(COMPILER_INCLUDE_DIR lib/cparser/${cparser_VERSION}/include CACHE STRING "compiler include dir (under CMAKE_INSTALL_PREFIX)")
86set(HOST_TRIPLE CACHE STRING "host machine triple (may be empty)")
87
88add_definitions(-DSYSTEM_INCLUDE_DIR=\"${SYSTEM_INCLUDE_DIR}\")
89add_definitions(-DLOCAL_INCLUDE_DIR=\"${LOCAL_INCLUDE_DIR}\")
90add_definitions(-DCOMPILER_INCLUDE_DIR=\"${CMAKE_INSTALL_PREFIX}/${COMPILER_INCLUDE_DIR}\")
91if(HOST_TRIPLE)
92  add_definitions(-DHOST_TRIPLE=\"${HOST_TRIPLE}\")
93endif()
94
95# Produce revision.h
96include_directories(${CMAKE_CURRENT_BINARY_DIR})
97if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/.git/HEAD)
98	set(REVISIONH "${CMAKE_CURRENT_BINARY_DIR}/revision.h")
99	set(REVGEN echo '\#define cparser_REVISION \"'`git describe --abbrev=40 --always --dirty --match ''`'\"')
100	add_custom_command(
101		OUTPUT ${REVISIONH}
102		DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/.git/HEAD
103		COMMAND ${REVGEN} | cmp -s - "${REVISIONH}" 2> /dev/null || ${REVGEN} > "${REVISIONH}"
104		WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
105	)
106	SET_SOURCE_FILES_PROPERTIES(src/driver/actions.c PROPERTIES OBJECT_DEPENDS ${REVISIONH})
107else()
108	file(GENERATE OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/revision.h
109		 CONTENT "#define cparser_REVISION \"\"")
110endif()
111
112# Installation
113set(INSTALL_HEADERS
114	include/float.h
115	include/iso646.h
116	include/limits.h
117	include/stdarg.h
118	include/stdbool.h
119	include/stddef.h
120)
121install(FILES ${INSTALL_HEADERS} DESTINATION ${COMPILER_INCLUDE_DIR})
122install(FILES cparser.1 DESTINATION share/man/man1)
123install(TARGETS cparser DESTINATION bin)
124