From: Xavier Del Campo Romero Date: Thu, 4 Jun 2026 14:05:42 +0000 (+0200) Subject: cobol: Move copybook path searches to cobol1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d70dbee952cd6aa956190e2993e14dd3b712efd3;p=thirdparty%2Fgcc.git cobol: Move copybook path searches to cobol1 Previously, this task was performed by the gcobol driver. However, prior art from the Modula-2 front-end showed this was performed by the compiler instead. Now, and similarly to Modula-2, cobol1 will honor -B, as well as -idirafter. While the %I spec is also honored, it will not be as useful because -B options would only be expanded to -isystem by %I if the "include/" and "include-fixed/" directories exist wherever -B points at. However, system copybooks are currently installed on: - $(libsubdir)/compat/gnu/cpy - $(libsubdir)/posix/cpy Thus following again Modula-2's example. TODO: Some of the -i* options, such as -imultilib, are not supported yet. Reportedly, only -I, -isystem and -idirafter are supported. gcc/cobol/ChangeLog: * Make-lang.in: Make $(libsubdir) available to cobol1.o. * cobol1.cc (struct GTY): Add dir_separator. (libcompat_copybook): New function. (libposix_copybook): New function. (append_copybook_prefix): New function. (cobol_langhook_handle_option): New function. (cobol_langhook_post_options): New function. (LANG_HOOKS_POST_OPTIONS): New function. * gcobolspec.cc (lang_specific_driver): Remove path searches. * lang-specs.h: Honor %{B*} and %I. * lang.opt: Support -B and -idirafter. libgcobol/ChangeLog: * Makefile.am: Install posix copybooks to build tree. * Makefile.in: Likewise. gcc/testsuite/ChangeLog: * cobol.dg/group2/cbltypes.cpy: Removed. --- diff --git a/gcc/cobol/Make-lang.in b/gcc/cobol/Make-lang.in index a5cfe2cea11..f2b6062ec65 100644 --- a/gcc/cobol/Make-lang.in +++ b/gcc/cobol/Make-lang.in @@ -125,6 +125,8 @@ cobol_OBJS = \ # CFLAGS-cobol/gcobolspec.o += $(DRIVER_DEFINES) +CFLAGS-cobol/cobol1.o += -DLIBSUBDIR=\"$(libsubdir)\" + # # This controls the build of the gcobol "driver" # diff --git a/gcc/cobol/cobol1.cc b/gcc/cobol/cobol1.cc index bf60b0b664e..0cc17019717 100644 --- a/gcc/cobol/cobol1.cc +++ b/gcc/cobol/cobol1.cc @@ -103,6 +103,8 @@ struct GTY (()) language_function int dummy; }; +static const char dir_separator[] = {DIR_SEPARATOR, 0}; + /* * Language hooks. */ @@ -367,6 +369,75 @@ enable_exceptions( bool enable ) { } } +static void +libcompat_copybook(const char *dir) + { + char *gnu = concat(dir, NULL); + + if (gnu[strlen(gnu) - 1] != DIR_SEPARATOR) + gnu = concat(gnu, dir_separator, NULL); + + gnu = concat(gnu, "compat", dir_separator, "gnu", NULL); + + char *paths[] = + { + concat(gnu, dir_separator, "lib", NULL), + concat(gnu, dir_separator, "cpy", NULL), + concat(gnu, dir_separator, "udf", NULL) + }; + + for (size_t i = 0; i < sizeof paths / sizeof *paths; i++) + { + // Inject the installation prefix paths to the libcompat copybooks. + copybook_directory_add(paths[i]); + free(paths[i]); + } + + free(gnu); + } + +static void +libposix_copybook(const char *dir) + { + char *posix = concat(dir, NULL); + + if (posix[strlen(posix) - 1] != DIR_SEPARATOR) + posix = concat(posix, dir_separator, NULL); + + posix = concat(posix, "posix", NULL); + + char *paths[] = + { + concat(posix, dir_separator, "cpy", NULL), + concat(posix, dir_separator, "udf", NULL), + }; + + for (size_t i = 0; i < sizeof paths / sizeof *paths; i++) + { + copybook_directory_add(paths[i]); + free(paths[i]); + } + + free(posix); + } + +static void +append_copybook_prefix(const char *prefix, void (*fn)(const char *)) + { + char *opts[] = + { + concat(prefix, dir_separator, "cobol", NULL), + concat(prefix, NULL), + NULL + }; + + for (char **s = opts; *s; ++s) + { + fn(*s); + free(*s); + } + } + void cobol_warning( cbl_diag_id_t id, int yn, bool ); void cobol_warning_suppress( cbl_dialect_t dialect ); @@ -386,6 +457,10 @@ cobol_langhook_handle_option (size_t scode, switch(code) { + case OPT_B: + append_copybook_prefix(arg, libcompat_copybook); + append_copybook_prefix(arg, libposix_copybook); + return true; case OPT_D: defined_cmd(arg); return true; @@ -397,7 +472,9 @@ cobol_langhook_handle_option (size_t scode, copybook_directory_add(arg); return true; case OPT_copyext: - copybook_extension_add(cobol_copyext); + case OPT_isystem: + case OPT_idirafter: + copybook_extension_add(arg); return true; case OPT_fexec_charset_: @@ -804,6 +881,14 @@ cobol_langhook_getdecls (void) return NULL; } +static bool +cobol_langhook_post_options (const char **pfilename ATTRIBUTE_UNUSED) + { + append_copybook_prefix(LIBSUBDIR, libcompat_copybook); + append_copybook_prefix(LIBSUBDIR, libposix_copybook); + return false; + } + char * cobol_name_mangler(const char *cobol_name_) { @@ -909,6 +994,7 @@ cobol_get_sarif_source_language(const char *) #undef LANG_HOOKS_GETDECLS #undef LANG_HOOKS_GLOBAL_BINDINGS_P #undef LANG_HOOKS_HANDLE_OPTION +#undef LANG_HOOKS_POST_OPTIONS #undef LANG_HOOKS_INIT #undef LANG_HOOKS_INIT_OPTIONS_STRUCT #undef LANG_HOOKS_NAME @@ -930,6 +1016,7 @@ cobol_get_sarif_source_language(const char *) #define LANG_HOOKS_INIT_OPTIONS_STRUCT cobol_langhook_init_options_struct #define LANG_HOOKS_HANDLE_OPTION cobol_langhook_handle_option +#define LANG_HOOKS_POST_OPTIONS cobol_langhook_post_options #define LANG_HOOKS_BUILTIN_FUNCTION cobol_langhook_builtin_function #define LANG_HOOKS_GETDECLS cobol_langhook_getdecls diff --git a/gcc/cobol/gcobolspec.cc b/gcc/cobol/gcobolspec.cc index c28173c4185..618ff93b573 100644 --- a/gcc/cobol/gcobolspec.cc +++ b/gcc/cobol/gcobolspec.cc @@ -507,11 +507,6 @@ lang_specific_driver (struct cl_decoded_option **in_decoded_options, } } - char dir_separator[] = {DIR_SEPARATOR, 0}, - *tooldir = concat (STANDARD_EXEC_PREFIX, DEFAULT_TARGET_MACHINE, - dir_separator, DEFAULT_TARGET_VERSION, - dir_separator, "cobol", NULL); - /* As described above, we have empirically noticed that when the command line explicitly specifies libgcobol.a as an input, a following -lgcobol causes the "on exit" functions of the library to be executed twice. This can @@ -543,21 +538,7 @@ lang_specific_driver (struct cl_decoded_option **in_decoded_options, } if( need_libcompat ) { - char *gnu = concat(tooldir, dir_separator, "compat", dir_separator, "gnu", NULL); add_arg_lib(COMPAT_LIBRARY, static_libcompat); - - // Inject the installation prefix paths to the libcompat copybooks. - // Note that these paths are inevitably leaked as append_option - // takes a const char *, but does not copy the string. - // Ideally, these paths could be constructed at preprocessor-time, - // but unfortunately DIR_SEPARATOR defines an integer, not a string. - // Maybe a DIR_SEPARATOR-like macro could be defined instead, but that - // can be fragile in terms of portability, and the usual practice in - // gcc is to dynamically define it as a 2-element array, anyway. - append_option(OPT_I, concat(gnu, dir_separator, "lib", NULL), 1); - append_option(OPT_I, concat(gnu, dir_separator, "cpy", NULL), 1); - append_option(OPT_I, concat(gnu, dir_separator, "udf", NULL), 1); - free(gnu); } if( need_libdl ) { @@ -569,14 +550,7 @@ lang_specific_driver (struct cl_decoded_option **in_decoded_options, } if( need_libposix ) { - char *posix = concat(tooldir, dir_separator, "posix", NULL); - add_arg_lib(POSIX_LIBRARY, static_libposix); - // Inject the paths to the libposix copybooks. - // As explained above, note that these paths are inevitably leaked. - append_option(OPT_I, concat(posix, dir_separator, "cpy", NULL), 1); - append_option(OPT_I, concat(posix, dir_separator, "udf", NULL), 1); - free(posix); } if( prior_main ) @@ -585,8 +559,6 @@ lang_specific_driver (struct cl_decoded_option **in_decoded_options, fatal_error(input_location, "%s", ach); } - free(tooldir); - // We now take the new_opt vector, and turn it into an array of // cl_decoded_option diff --git a/gcc/cobol/lang-specs.h b/gcc/cobol/lang-specs.h index e82e6ca39fe..9fbc15d1cb9 100644 --- a/gcc/cobol/lang-specs.h +++ b/gcc/cobol/lang-specs.h @@ -34,7 +34,9 @@ {".CBL", "@cobol", 0, 0, 0}, {"@cobol", "cobol1 %i %(cc1_options) " - "%{D*} %{E} %{I*} %{M} %{fmax-errors*} %{fsyntax-only*} " + "%I " + "%{B*} %{D*} %{E} %{I*} %{M} %{fmax-errors*} %{fsyntax-only*} " + "%{idirafter}" "%{fcobol-exceptions*} " "%{copyext} " "%{fdefaultbyte} " diff --git a/gcc/cobol/lang.opt b/gcc/cobol/lang.opt index 2fd4d3571b0..f72277da1ca 100644 --- a/gcc/cobol/lang.opt +++ b/gcc/cobol/lang.opt @@ -25,6 +25,10 @@ Language Cobol +B +Cobol +; Documented in c.opt + D Cobol Joined Separate ; Documented in c.opt @@ -395,6 +399,10 @@ isystem Cobol Joined Separate ; Documented in C +idirafter +Cobol Joined Separate +; Documented in c.opt + main Cobol -main The first program-id in the next source file is called by a generated main() entry point. diff --git a/gcc/testsuite/cobol.dg/group2/cbltypes.cpy b/gcc/testsuite/cobol.dg/group2/cbltypes.cpy deleted file mode 100644 index 384bc0bdb17..00000000000 --- a/gcc/testsuite/cobol.dg/group2/cbltypes.cpy +++ /dev/null @@ -1,19 +0,0 @@ - >> PUSH source format - >>SOURCE format is fixed - - * The information is returned to the file-info argument, which - * is defined as the following 16-byte area: - - 01 cblt-fileexist-buf typedef. - 03 cblte-fe-filesize PIC X(8) COMP-X. - 03 cblte-fe-date. - 05 cblte-fe-day PIC X COMP-X. - 05 cblte-fe-month PIC X COMP-X. - 05 cblte-fe-year PIC X(2) comp-x. - 03 cblte-fe-time. - 05 cblte-fe-hours PIC X COMP-X. - 05 cblte-fe-minutes PIC X COMP-X. - 05 cblte-fe-seconds PIC X COMP-X. - 05 cblte-fe-hundreths PIC X COMP-X. - - >> POP source format diff --git a/libgcobol/Makefile.am b/libgcobol/Makefile.am index 19a47d7d136..4efbcee119f 100644 --- a/libgcobol/Makefile.am +++ b/libgcobol/Makefile.am @@ -157,12 +157,34 @@ $(LC_COPYDIR_BUILD)/%.cpy: ${srcdir}/compat/gnu/cpy/%.cpy mkdir -p `dirname $@` cp $< $@ +# Bring posix copybooks into the build tree. +LP_COPYDIR_BUILD = $(PWD)/posix/cpy +LP_COPYBOOKS = \ + $(LP_COPYDIR_BUILD)/posix-errno.cpy \ + $(LP_COPYDIR_BUILD)/posix-exit.cpy \ + $(LP_COPYDIR_BUILD)/posix-fstat.cpy \ + $(LP_COPYDIR_BUILD)/posix-ftruncate.cpy \ + $(LP_COPYDIR_BUILD)/posix-localtime.cpy \ + $(LP_COPYDIR_BUILD)/posix-lseek.cpy \ + $(LP_COPYDIR_BUILD)/posix-mkdir.cpy \ + $(LP_COPYDIR_BUILD)/posix-open.cpy \ + $(LP_COPYDIR_BUILD)/posix-read.cpy \ + $(LP_COPYDIR_BUILD)/posix-stat.cpy \ + $(LP_COPYDIR_BUILD)/psx-lseek.cpy \ + $(LP_COPYDIR_BUILD)/psx-open.cpy \ + $(LP_COPYDIR_BUILD)/statbuf.cpy \ + $(LP_COPYDIR_BUILD)/tm.cpy + +$(LP_COPYDIR_BUILD)/%.cpy: ${srcdir}/posix/cpy/%.cpy + mkdir -p `dirname $@` + cp $< $@ + # We want to link with the c++ runtime. libgcobol_la_LINK = $(CXXLINK) $(libgcobol_la_LDFLAGS) version_arg = -version-info $(LIBGCOBOL_VERSION) libgcobol_la_LDFLAGS = $(LTLDFLAGS) $(LIBQUADLIB) $(LTLIBICONV) \ $(extra_ldflags_libgcobol) $(LIBS) $(LIBXML2_LIBS) $(version_arg) -libgcobol_la_DEPENDENCIES = libgcobol.spec $(LIBQUADLIB_DEP) $(LC_COPYBOOKS) +libgcobol_la_DEPENDENCIES = libgcobol.spec $(LIBQUADLIB_DEP) # Rules for libgcobol_posix.so and libgcobol_compat_gnu.so, which have # COBOL sources. They require gcobol and libgcobol to have already @@ -178,12 +200,14 @@ libgcobol_la_DEPENDENCIES = libgcobol.spec $(LIBQUADLIB_DEP) $(LC_COPYBOOKS) libgcobol_posix_la_LINK = $(CXXLINK) $(libgcobol_posix_la_LDFLAGS) libgcobol_posix_la_LDFLAGS = $(LTLDFLAGS) $(LIBQUADLIB) $(LTLIBICONV) \ $(extra_ldflags_libgcobol) $(LIBS) $(LIBXML2_LIBS) $(version_arg) -libgcobol_posix_la_DEPENDENCIES = libgcobol.spec $(LIBQUADLIB_DEP) +libgcobol_posix_la_DEPENDENCIES = libgcobol.spec $(LIBQUADLIB_DEP) \ + $(LP_COPYBOOKS) libgcobol_compat_gnu_la_LINK = $(CXXLINK) $(libgcobol_compat_gnu_la_LDFLAGS) libgcobol_compat_gnu_la_LDFLAGS = $(LTLDFLAGS) $(LIBQUADLIB) $(LTLIBICONV) \ $(extra_ldflags_libgcobol) $(LIBS) $(LIBXML2_LIBS) $(version_arg) -libgcobol_compat_gnu_la_DEPENDENCIES = libgcobol.spec $(LIBQUADLIB_DEP) +libgcobol_compat_gnu_la_DEPENDENCIES = libgcobol.spec $(LIBQUADLIB_DEP) \ + $(LC_COPYBOOKS) LTCOBCOMPILE = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) \ diff --git a/libgcobol/Makefile.in b/libgcobol/Makefile.in index 53d4d12d014..e1286e3ded2 100644 --- a/libgcobol/Makefile.in +++ b/libgcobol/Makefile.in @@ -569,13 +569,32 @@ gcc_version := $(shell @get_gcc_base_ver@ $(top_srcdir)/../gcc/BASE-VER) @BUILD_LIBGCOBOL_TRUE@ $(LC_COPYDIR_BUILD)/stored-char-length.cpy +# Bring posix copybooks into the build tree. +@BUILD_LIBGCOBOL_TRUE@LP_COPYDIR_BUILD = $(PWD)/posix/cpy +@BUILD_LIBGCOBOL_TRUE@LP_COPYBOOKS = \ +@BUILD_LIBGCOBOL_TRUE@ $(LP_COPYDIR_BUILD)/posix-errno.cpy \ +@BUILD_LIBGCOBOL_TRUE@ $(LP_COPYDIR_BUILD)/posix-exit.cpy \ +@BUILD_LIBGCOBOL_TRUE@ $(LP_COPYDIR_BUILD)/posix-fstat.cpy \ +@BUILD_LIBGCOBOL_TRUE@ $(LP_COPYDIR_BUILD)/posix-ftruncate.cpy \ +@BUILD_LIBGCOBOL_TRUE@ $(LP_COPYDIR_BUILD)/posix-localtime.cpy \ +@BUILD_LIBGCOBOL_TRUE@ $(LP_COPYDIR_BUILD)/posix-lseek.cpy \ +@BUILD_LIBGCOBOL_TRUE@ $(LP_COPYDIR_BUILD)/posix-mkdir.cpy \ +@BUILD_LIBGCOBOL_TRUE@ $(LP_COPYDIR_BUILD)/posix-open.cpy \ +@BUILD_LIBGCOBOL_TRUE@ $(LP_COPYDIR_BUILD)/posix-read.cpy \ +@BUILD_LIBGCOBOL_TRUE@ $(LP_COPYDIR_BUILD)/posix-stat.cpy \ +@BUILD_LIBGCOBOL_TRUE@ $(LP_COPYDIR_BUILD)/psx-lseek.cpy \ +@BUILD_LIBGCOBOL_TRUE@ $(LP_COPYDIR_BUILD)/psx-open.cpy \ +@BUILD_LIBGCOBOL_TRUE@ $(LP_COPYDIR_BUILD)/statbuf.cpy \ +@BUILD_LIBGCOBOL_TRUE@ $(LP_COPYDIR_BUILD)/tm.cpy + + # We want to link with the c++ runtime. @BUILD_LIBGCOBOL_TRUE@libgcobol_la_LINK = $(CXXLINK) $(libgcobol_la_LDFLAGS) @BUILD_LIBGCOBOL_TRUE@version_arg = -version-info $(LIBGCOBOL_VERSION) @BUILD_LIBGCOBOL_TRUE@libgcobol_la_LDFLAGS = $(LTLDFLAGS) $(LIBQUADLIB) $(LTLIBICONV) \ @BUILD_LIBGCOBOL_TRUE@ $(extra_ldflags_libgcobol) $(LIBS) $(LIBXML2_LIBS) $(version_arg) -@BUILD_LIBGCOBOL_TRUE@libgcobol_la_DEPENDENCIES = libgcobol.spec $(LIBQUADLIB_DEP) $(LC_COPYBOOKS) +@BUILD_LIBGCOBOL_TRUE@libgcobol_la_DEPENDENCIES = libgcobol.spec $(LIBQUADLIB_DEP) # Rules for libgcobol_posix.so and libgcobol_compat_gnu.so, which have # COBOL sources. They require gcobol and libgcobol to have already @@ -590,12 +609,16 @@ gcc_version := $(shell @get_gcc_base_ver@ $(top_srcdir)/../gcc/BASE-VER) @BUILD_LIBGCOBOL_TRUE@libgcobol_posix_la_LDFLAGS = $(LTLDFLAGS) $(LIBQUADLIB) $(LTLIBICONV) \ @BUILD_LIBGCOBOL_TRUE@ $(extra_ldflags_libgcobol) $(LIBS) $(LIBXML2_LIBS) $(version_arg) -@BUILD_LIBGCOBOL_TRUE@libgcobol_posix_la_DEPENDENCIES = libgcobol.spec $(LIBQUADLIB_DEP) +@BUILD_LIBGCOBOL_TRUE@libgcobol_posix_la_DEPENDENCIES = libgcobol.spec $(LIBQUADLIB_DEP) \ +@BUILD_LIBGCOBOL_TRUE@ $(LP_COPYBOOKS) + @BUILD_LIBGCOBOL_TRUE@libgcobol_compat_gnu_la_LINK = $(CXXLINK) $(libgcobol_compat_gnu_la_LDFLAGS) @BUILD_LIBGCOBOL_TRUE@libgcobol_compat_gnu_la_LDFLAGS = $(LTLDFLAGS) $(LIBQUADLIB) $(LTLIBICONV) \ @BUILD_LIBGCOBOL_TRUE@ $(extra_ldflags_libgcobol) $(LIBS) $(LIBXML2_LIBS) $(version_arg) -@BUILD_LIBGCOBOL_TRUE@libgcobol_compat_gnu_la_DEPENDENCIES = libgcobol.spec $(LIBQUADLIB_DEP) +@BUILD_LIBGCOBOL_TRUE@libgcobol_compat_gnu_la_DEPENDENCIES = libgcobol.spec $(LIBQUADLIB_DEP) \ +@BUILD_LIBGCOBOL_TRUE@ $(LC_COPYBOOKS) + @BUILD_LIBGCOBOL_TRUE@LTCOBCOMPILE = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) \ @BUILD_LIBGCOBOL_TRUE@ --mode=compile $(AM_COBC) $(AM_COBFLAGS) @@ -1104,6 +1127,10 @@ uninstall-am: uninstall-nobase_libsubincludeHEADERS \ @BUILD_LIBGCOBOL_TRUE@ mkdir -p `dirname $@` @BUILD_LIBGCOBOL_TRUE@ cp $< $@ +@BUILD_LIBGCOBOL_TRUE@$(LP_COPYDIR_BUILD)/%.cpy: ${srcdir}/posix/cpy/%.cpy +@BUILD_LIBGCOBOL_TRUE@ mkdir -p `dirname $@` +@BUILD_LIBGCOBOL_TRUE@ cp $< $@ + @BUILD_LIBGCOBOL_TRUE@.cbl.o: @BUILD_LIBGCOBOL_TRUE@ $(COBC) -o $@ $(COBFLAGS) -c $<