]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/nvi/CMakeLists.txt
Add UPDATING entries and bump version.
[FreeBSD/FreeBSD.git] / contrib / nvi / CMakeLists.txt
1 cmake_minimum_required(VERSION 3.9)
2
3 get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
4 if(is_multi_config)
5     set(CMAKE_CONFIGURATION_TYPES Debug Release CACHE STRING
6         "Semicolon separated list of supported configuration types")
7     mark_as_advanced(CMAKE_CONFIGURATION_TYPES)
8 elseif(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_C_FLAGS)
9     message(WARNING "No CMAKE_BUILD_TYPE is selected")
10 endif()
11
12 project(nvi2 C)
13
14 include(CheckIncludeFiles)
15 include(CheckFunctionExists)
16 include(CheckSymbolExists)
17 include(CheckStructHasMember)
18 include(CheckCSourceCompiles)
19 include(CheckCCompilerFlag)
20
21 mark_as_advanced(CMAKE_INSTALL_PREFIX)
22
23 option(USE_WIDECHAR "Enable wide character support" ON)
24 option(USE_ICONV "Enable iconv support" ON)
25
26 check_c_compiler_flag(-fcolor-diagnostics USE_FCOLOR_DIAGNOSTICS)
27 if(USE_FCOLOR_DIAGNOSTICS)
28     add_compile_options(-fcolor-diagnostics)
29 endif()
30
31 add_compile_options($<$<CONFIG:Debug>:-Wall>)
32 add_compile_options($<$<CONFIG:Debug>:-Wno-parentheses>)
33 add_compile_options($<$<CONFIG:Debug>:-Wno-uninitialized>)
34 add_compile_options($<$<CONFIG:Debug>:-Wmissing-prototypes>)
35 if (NOT APPLE)
36     add_compile_options($<$<CONFIG:Debug>:-Wsystem-headers>)
37 endif()
38 add_compile_options($<$<CONFIG:Release>:-Wuninitialized>)
39 add_compile_options($<$<CONFIG:Release>:-Wno-dangling-else>)
40 add_compile_options(-Wstack-protector -fstack-protector)
41 add_compile_options(-Wstrict-aliasing -fstrict-aliasing)
42
43 include_directories(${CMAKE_CURRENT_BINARY_DIR})
44
45 set(MAIN_PROTOS
46     cl/extern.h common/extern.h ex/extern.h vi/extern.h
47     common/options_def.h ex/ex_def.h ex/version.h)
48
49 set(CL_SRCS
50     cl/cl_funcs.c cl/cl_main.c cl/cl_read.c cl/cl_screen.c cl/cl_term.c)
51
52 set(COMMON_SRCS
53     common/conv.c common/cut.c common/delete.c common/encoding.c common/exf.c
54     common/key.c common/line.c common/log.c common/main.c common/mark.c
55     common/msg.c common/options.c common/options_f.c common/put.c
56     common/recover.c common/screen.c common/search.c common/seq.c
57     common/util.c)
58
59 set(EX_SRCS
60     ex/ex.c ex/ex_abbrev.c ex/ex_append.c ex/ex_args.c ex/ex_argv.c ex/ex_at.c
61     ex/ex_bang.c ex/ex_cd.c ex/ex_cmd.c ex/ex_cscope.c ex/ex_delete.c
62     ex/ex_display.c ex/ex_edit.c ex/ex_equal.c ex/ex_file.c ex/ex_filter.c
63     ex/ex_global.c ex/ex_init.c ex/ex_join.c ex/ex_map.c ex/ex_mark.c
64     ex/ex_mkexrc.c ex/ex_move.c ex/ex_open.c ex/ex_preserve.c ex/ex_print.c
65     ex/ex_put.c ex/ex_quit.c ex/ex_read.c ex/ex_screen.c ex/ex_script.c
66     ex/ex_set.c ex/ex_shell.c ex/ex_shift.c ex/ex_source.c ex/ex_stop.c
67     ex/ex_subst.c ex/ex_tag.c ex/ex_txt.c ex/ex_undo.c ex/ex_usage.c
68     ex/ex_util.c ex/ex_version.c ex/ex_visual.c ex/ex_write.c ex/ex_yank.c
69     ex/ex_z.c)
70
71 set(VI_SRCS
72     vi/getc.c vi/v_at.c vi/v_ch.c vi/v_cmd.c vi/v_delete.c vi/v_ex.c
73     vi/v_increment.c vi/v_init.c vi/v_itxt.c vi/v_left.c vi/v_mark.c
74     vi/v_match.c vi/v_paragraph.c vi/v_put.c vi/v_redraw.c vi/v_replace.c
75     vi/v_right.c vi/v_screen.c vi/v_scroll.c vi/v_search.c vi/v_section.c
76     vi/v_sentence.c vi/v_status.c vi/v_txt.c vi/v_ulcase.c vi/v_undo.c
77     vi/v_util.c vi/v_word.c vi/v_xchar.c vi/v_yank.c vi/v_z.c vi/v_zexit.c
78     vi/vi.c vi/vs_line.c vi/vs_msg.c vi/vs_refresh.c vi/vs_relative.c
79     vi/vs_smap.c vi/vs_split.c)
80
81 set(REGEX_SRCS
82     regex/regcomp.c regex/regerror.c regex/regexec.c regex/regfree.c)
83
84 # commands to generate the public headers
85 set(extract_protos sed -n 's/^ \\* PUBLIC: \\\(.*\\\)/\\1/p')
86 set(extract_version sed -n
87     's/^.*version \\\([^\)]*\)\\\).*/\#define VI_VERSION \\\"\\1\\\"/p')
88
89 add_custom_command(OUTPUT cl/extern.h
90                    COMMAND ${extract_protos} ${CL_SRCS} > cl/extern.h
91                    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
92                    DEPENDS ${CL_SRCS})
93 add_custom_command(OUTPUT common/extern.h
94                    COMMAND ${extract_protos} ${COMMON_SRCS} > common/extern.h
95                    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
96                    DEPENDS ${COMMON_SRCS})
97 add_custom_command(OUTPUT ex/extern.h
98                    COMMAND ${extract_protos} ${EX_SRCS} > ex/extern.h
99                    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
100                    DEPENDS ${EX_SRCS})
101 add_custom_command(OUTPUT vi/extern.h
102                    COMMAND ${extract_protos} ${VI_SRCS} > vi/extern.h
103                    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
104                    DEPENDS ${VI_SRCS})
105 add_custom_command(OUTPUT common/options_def.h
106                    COMMAND awk -f common/options.awk
107                            common/options.c > common/options_def.h
108                    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
109                    DEPENDS common/options.c)
110 add_custom_command(OUTPUT ex/ex_def.h
111                    COMMAND awk -f ex/ex.awk ex/ex_cmd.c > ex/ex_def.h
112                    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
113                    DEPENDS ex/ex_cmd.c)
114 add_custom_command(OUTPUT ex/version.h
115                    COMMAND ${extract_version} README > ex/version.h
116                    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
117                    DEPENDS README)
118
119 add_executable(nvi)
120 target_sources(nvi PRIVATE ${MAIN_PROTOS} ${CL_SRCS} ${COMMON_SRCS}
121                            ${EX_SRCS} ${VI_SRCS})
122 target_compile_definitions(nvi PRIVATE $<$<CONFIG:Debug>:DEBUG>
123                                        $<$<CONFIG:Debug>:COMLOG>)
124
125 check_function_exists(openpty UTIL_IN_LIBC)
126 if(NOT UTIL_IN_LIBC)
127     find_library(UTIL_LIBRARY util)
128     target_link_libraries(nvi PRIVATE ${UTIL_LIBRARY})
129 endif()
130
131 check_function_exists(__b64_ntop RESOLV_IN_LIBC)
132 if(NOT RESOLV_IN_LIBC)
133     find_library(RESOLV_LIBRARY resolv)
134     target_link_libraries(nvi PRIVATE ${RESOLV_LIBRARY})
135 endif()
136
137 check_symbol_exists(asprintf "stdio.h" ASPRINTF_IN_STDIO_H)
138 if(NOT ASPRINTF_IN_STDIO_H)
139     target_compile_definitions(nvi PRIVATE _GNU_SOURCE)
140 endif()
141
142 if(USE_WIDECHAR)
143     find_library(CURSES_LIBRARY NAMES ncursesw cursesw curses HINTS /usr/lib)
144     find_library(TERMINFO_LIBRARY NAMES tinfow terminfo HINTS /usr/lib)
145
146     # link to the wchar_t awared BSD libregex.a
147     add_library(regex STATIC)
148     target_sources(regex PRIVATE ${REGEX_SRCS})
149     target_include_directories(regex PUBLIC regex)
150     target_compile_definitions(regex PUBLIC __REGEX_PRIVATE)
151     target_link_libraries(nvi PRIVATE regex)
152 else()
153     find_library(CURSES_LIBRARY NAMES ncurses curses HINTS /usr/lib)
154     find_library(TERMINFO_LIBRARY NAMES tinfo terminfo HINTS /usr/lib)
155     target_compile_options(nvi PRIVATE -Wno-pointer-sign)
156 endif()
157
158 target_link_libraries(nvi PRIVATE ${CURSES_LIBRARY})
159 if(TERMINFO_LIBRARY)
160     target_link_libraries(nvi PRIVATE ${TERMINFO_LIBRARY})
161 endif()
162
163 if(USE_ICONV)
164     check_function_exists(iconv ICONV_IN_LIBC)
165     if(NOT ICONV_IN_LIBC)
166         find_path(ICONV_INCLUDE_DIR iconv.h)
167         find_library(ICONV_LIBRARY iconv)
168     endif()
169
170     # detect the prototype of iconv(3)
171     set(CMAKE_C_FLAGS_BACKUP "${CMAKE_C_FLAGS}")
172     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror")
173     set(CMAKE_REQUIRED_INCLUDES "${ICONV_INCLUDE_DIR}")
174     set(CMAKE_REQUIRED_LIBRARIES "${ICONV_LIBRARY}")
175     check_c_source_compiles("
176     #include <iconv.h>
177     int main() {
178         iconv_t conv = 0;
179         char* in = 0;
180         size_t ilen = 0;
181         char* out = 0;
182         size_t olen = 0;
183         iconv(conv, &in, &ilen, &out, &olen);
184         return 0;
185     }
186     " ICONV_TRADITIONAL)
187     set(CMAKE_REQUIRED_INCLUDES)
188     set(CMAKE_REQUIRED_LIBRARIES)
189     set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS_BACKUP}")
190
191     target_include_directories(nvi PRIVATE ${ICONV_INCLUDE_DIR})
192     target_link_libraries(nvi PRIVATE ${ICONV_LIBRARY})
193 endif()
194
195 check_function_exists(getprogname GETPROGNAME_IN_LIBC)
196 check_function_exists(strlcpy STRLCPY_IN_LIBC)
197 if(NOT GETPROGNAME_IN_LIBC OR NOT STRLCPY_IN_LIBC)
198     find_package(PkgConfig REQUIRED)
199     pkg_check_modules(LIBBSD libbsd-overlay)
200     add_definitions(${LIBBSD_CFLAGS})
201     target_link_libraries(nvi PRIVATE ${LIBBSD_LIBRARIES})
202 endif()
203
204 check_function_exists(dbopen DBOPEN_IN_LIBC)
205 if(NOT DBOPEN_IN_LIBC)
206     target_link_libraries(nvi PRIVATE db1)
207 endif()
208 if (APPLE)
209     # Avoid using an incompatible db.h installed to /usr/local (since this is
210     # part of the default search path on macOS)
211     set(DB_H_GUESS "${CMAKE_OSX_SYSROOT}/usr/include/db.h")
212     if (NOT EXISTS ${DB_H_GUESS})
213         message(FATAL_ERROR "Could not find db.h at the expected path (${DB_H_GUESS}).")
214     endif()
215     add_definitions("-DDB_H_ABS_PATH=<${DB_H_GUESS}>")
216 else()
217     find_path(DB_INCLUDE_DIR db.h PATH_SUFFIXES db1)
218     target_include_directories(nvi PRIVATE ${DB_INCLUDE_DIR})
219 endif()
220
221 check_include_files(libutil.h HAVE_LIBUTIL_H)
222 check_include_files(ncurses.h HAVE_NCURSES_H)
223 check_include_files(ncursesw/ncurses.h HAVE_NCURSESW_NCURSES_H)
224 check_include_files(pty.h HAVE_PTY_H)
225 check_include_files(term.h HAVE_TERM_H)
226 check_struct_has_member("struct dirent" d_namlen dirent.h HAVE_DIRENT_D_NAMLEN LANGUAGE C)
227 check_struct_has_member("struct stat" st_mtimespec
228     "sys/types.h;sys/stat.h" HAVE_STRUCT_STAT_ST_MTIMESPEC LANGUAGE C)
229 check_struct_has_member("struct stat" st_mtim
230     "sys/types.h;sys/stat.h" HAVE_STRUCT_STAT_ST_MTIM LANGUAGE C)
231
232 configure_file(files/config.h.in config.h)
233
234 set(vi_cv_path_preserve /var/tmp/vi.recover/)
235 if(APPLE)
236     set(vi_cv_path_msgcat /usr/local/share/vi/catalog/)
237 else()
238     set(vi_cv_path_msgcat /usr/share/vi/catalog/)
239 endif()
240
241 configure_file(files/pathnames.h.in pathnames.h)
242 configure_file(files/recover.in recover @ONLY)