Menu

#450 Configurable build targets (cross-compilation)

unclassified
open
nobody
None
5 - default
2023-10-18
2023-10-11
No

At the moment, GnuCobol can only compile COBOL program for the machine it was compiled for.

I've been doing some experiments trying to build a GnuCobol "cross compiler" that runs on Windows and produces Linux binaries. It was quite hackish but I managed to achieve it, by manually tweaking the detected configuration in config.h and setting the various COB_XXX variables in a way suitable for my environment (not mentioning I also had to build a Windows-to-Linux C cross-compiler).

It would be a nice addition to make this process easier. It would probably require moving many compiler-specific code (currently handled by #ifdefs) to dedicated configuration files, for instance in a config/compiler.cfg file (similarly to what we have with config/runtime.cfg). Of course that also means we have to be able to build libcob for the various targets we intend to have.

Discussion

  • Simon Sobisch

    Simon Sobisch - 2023-10-18

    While this is a different approach than [feature-requests:#245] it is still related. This related FR was about a "constant" cross-compiling environment (create a cross-compiler), while this FR here is about making compilation targets totally cobc-runtime dependent.
    The "common" option is the first one, so if you want to have a compiler that has multiple targets, then you distribute multiple executable, which possibly share the same configuration files.

    There are rare exceptions like OpenWatcom where the target may be set via command line option (it is not unlikely that this is just done in the frontend and depending on the target different binaries are executed - I haven't checked that).

    At the moment, GnuCobol can only compile COBOL program for the machine it was compiled for.

    That's not 100% correct. If you use a compatible C compiler (like recent GCC for 64bit on both GNU/Linux and Windows) and the GnuCOBOL configuration in both versions have the same configuration then you can cross-compile by "just" changing COBC_CC to point to the cross compiler and adding the path to the cross-compiled library by -L (or adjust COB_LIB_PATHS and if necessary), same for -I (or COB_CFLAGS).

    A compiler target configuration file will still need the same minimal setup (libcob in target to link to, C cross compiler), it "only" makes a difference for how those 2 to 3 options are set.

    It would be right to say that GnuCOBOL cannot handle a completely different compiler (two C compilers that differ too much by features) or a cross target that has a different configuration, but is this something that actually is a problem that exist?

     

    Related

    Wish List: #245


    Last edit: Simon Sobisch 2023-10-18
    • David Declerck

      David Declerck - 2023-10-18

      It would be right to say that GnuCOBOL cannot handle a completely different compiler (two C compilers that differ too much by features) or a cross target that has a different configuration, but is this something that actually is a problem that exist?

      Not sure if that could actually be useful, but it's just that I have a thing for cross-compilation these days. Though having cross-compilation in mind when writing code also help making it more portable by anticipating where compiler variations could be a problem.

      At the moment, GnuCobol can only compile COBOL program for the machine it was compiled for.
      That's not 100% correct. If you use a compatible C compiler (like recent GCC for 64bit on both GNU/Linux and Windows) and the GnuCOBOL configuration in both versions have the same configuration then you can cross-compile by "just" changing COBC_CC to point to the cross compiler and adding the path to the cross-compiled library by -L (or adjust COB_LIB_PATHS and if necessary), same for -I (or COB_CFLAGS).

      What I meant was that it is not completely automatic, and can even be tricky under some settings. TBH I was under a very particular setting when I tried : I was actually cross-compiling from Linux to Windows a GnuCobol cross-compiler from Windows to Linux (please don't ask...). So it implied manually tweaking the auto-detected configuration in config.h (things like COB_CC, COB_CFLAGS, COB_EXE_EXT, COB_EXPORT_DYN, COB_MODULE_EXT...), because I didn't want to set those via environment variables. I actually built two compilers that work on Windows : a cross-compiler to Linux, and a native compiler to Windows (hence two different executables and runtime libraries) - so I indeed went for the "constant" cross-compiling environment you mentioned.

       

Log in to post a comment.