Hello, On Tue, 29 Nov 2022, James K. Lowden wrote:
> I don't understand how to access in a front end the arguments to the -I > option on the command line. > > Cobol has a feature similar to the C preprecessor, known as the > Compiler Directing Facility (CDF). The CDF has a COPY statement that > resembles an #include directive in C, and shares the property that COPY > names a file that is normally found in a "copybook" which, for our > purposes, is a directory of such files. The name of that directory is > defined outside the Cobol program. > > I would like to use the -I option to pass the names of copybook > directories to the cobol front end. A bit of exploration yesterday left > me with the sense that the -I argument, in C at least, is not passed to > the compiler, but to the preprocessor. Access to -fmax-errors I think > I've figured out, but -I is a mystery. > > I'm a little puzzled by the status quo as I understand it. Unless I > missed it, it's not discussed in gccint. ISTM ideally there would be > some kind of getopt(3) processing, and the whole set of command-line > options captured in an array of structures accessible to any front > end. There is, it's just much more complicated than getopt :) If you're looking at the C frontends for inspiration, then: c-family/c.opt defines which options are recognized and several specifics about them, e.g. for -I it has: ---- I C ObjC C++ ObjC++ Joined Separate MissingArgError(missing path after %qs) -I <dir> Add <dir> to the end of the main include path. ---- (look at some other examples therein, also in common.opt to get a feel). Then code in c-family/c-opts.c:c_common_handle_option actually handles the option: case OPT_I: if (strcmp (arg, "-")) add_path (xstrdup (arg), INC_BRACKET, 0, true); else .,. That function is made a langhook for option processing so that it's actually called via c/c-objc-common.h: #define LANG_HOOKS_HANDLE_OPTION c_common_handle_option If you're also using the model of a compiler driver (i.e. the gcc program, source in gcc.cc) that actually calls compiler (cc1), assembler and linker, then you also need to arrange for that program to pass all -I options to the compiler proper. That is done with the spec language, by somewhere having '{I*}' in the specs for invoking the cobol compiler. E.g. look in gcc.cc for '@c' (matching the file extension) how that entry uses '%(cpp_unique_options)', and how cpp_unique_options is defined for the specs language: INIT_STATIC_SPEC ("cpp_unique_options", &cpp_unique_options), and static const char *cpp_unique_options = "%{!Q:-quiet} %{nostdinc*} %{C} %{CC} %{v} %@{I*&F*} %{P} %I\ (the specs language used here is documented in a lengthy comment early in gcc.cc, "The Specs Language") The "%@{I*F*}" is the one that makes gcc pass -Iwhatever to cc1 (and ensures relative order with -F options is retained and puts all these into an @file if one is given on the cmdline, otherwise leaves it on cmdline). If you use the compiler driver then using '-v' when invoking it will quickly tell you if that options passing worked, as it will show the concrete command it exec's for the compiler proper. Hope this helps. Ciao, Michael.