1

On W32 (and when cross-compiling for W32 from a linux system) I'd like to have automake build two executables from the same Makefile.am that differ in their extension. E.g. - foo.exe - foo.com

Since automake magically handles the extension, i don't really see how to achieve that. E.g.

bin_PROGRAMS=foo foo.com
foo_SOURCES=foo.c
foo_com_SOURCES=foo.c

Will happily produce two binaries foo.exe and foo.com.exe :-(

The best I've come up so far with is:

AUTOMAKE_OPTIONS = no-exeext

bin_PROGRAMS=foo
noinst_PROGRAMS=foocom
bin_SCRIPTS=foo.com

foo_SOURCES=foo.c flub.c
foocom_SOURCES=foo.c knark.c

clean-local:
    -rm -rf foo.com
foo.com: foocom$(EXEEXT)
    cp $^ $@

Could I expect more?

Background

I'm building a cross-platform application (let's call it foo), with an automake-based buildsystem.

On un*x-like systems, the application stays in the foreground (and outputs to stdout/stderr). On W32 this is usually unwanted (as it would require the application to be a Console Application with an ugly console-window to stay open while the application is running). Sometimes it is wanted though (e.g. for debugging). The solution currently applied (with a non-automake based build-system on W32) is to build the same application both as foo.exe (a native W32 application), and foo.com (a Console Application).

umläute
  • 28,885
  • 9
  • 68
  • 122
  • Whats wrong with having `WinMain` for Windows and `main` for the rest? This will suppress console window on startup. More info is [here](https://stackoverflow.com/questions/224225/create-an-application-without-a-window) and [here](https://stackoverflow.com/questions/7474504/compiling-a-win32-gui-app-without-a-console-using-mingw-and-eclipse/). – Andrejs Cainikovs Jun 08 '17 at 14:13
  • `foo.exe` uses `WinMain`, and `foo.com` uses `main` and creates lots of debugging output on the stderr. The question is not about how to avoid creating the console (that is solved), but about how to create both `foo.exe` and `foo.com`. – umläute Jun 08 '17 at 14:25
  • Just to clarify for someone unfamiliar with the current situation on Windows: Your `foo.com` file contains an EXE executable, but uses a `.com` filename extension? Is that considered good practise? My intuition would be to call that executable `foo-debug.exe` or something similar. – ndim Jun 20 '17 at 12:10
  • The approach has been suggested in the [MSDN Magazine 2004/02](http://download.microsoft.com/download/3/a/7/3a7fa450-1f33-41f7-9e6d-3aa95b5a6aea/MSDNMagazineFebruary2004en-us.chm), I guess it is good practice. One nice side-effect is, that if you are running the `foo` (without an extension) in the CLI, it will automatically pick up `foo.com` (which - in our case - gives the `main()` behaviour, something appropriate when running on the cmdline) – umläute Jun 21 '17 at 08:52
  • Good to know. Warning for everybody trying to run `foo.com` using Wine instead of native Windows: Wine determines the executable type of `foo.com` by its name alone as a DOS `.com` executable, not as a PE executable. Testing `foo.com` using Wine will thus fail, i.e. you will need to test the `foo-console.exe` instead. – ndim Aug 20 '17 at 17:04

1 Answers1

1

I would basically use the best you have come up with with a few quite small changes:

AM_INSTALLCHECK_STD_OPTIONS_EXEMPT  =
CLEANFILES                          =
bin_PROGRAMS                        =
noinst_PROGRAMS                     =
bin_SCRIPTS                         =

common_sources                      =
common_sources                     += foo.c

if BUILD_FOR_WINDOWS

AM_INSTALLCHECK_STD_OPTIONS_EXEMPT += foo$(EXEEXT)
bin_PROGRAMS                       += foo
foo_CFLAGS                          = -mwindows
foo_SOURCES                         = foo-windows.c
foo_SOURCES                        += $(common_sources)

AM_INSTALLCHECK_STD_OPTIONS_EXEMPT += foo-console$(EXEEXT)
noinst_PROGRAMS                    += foo-console
foo_console_CFLAGS                  = -mconsole
foo_console_SOURCES                 = foo-console.c
foo_console_SOURCES                += $(common_sources)

CLEANFILES                         += foo.com
AM_INSTALLCHECK_STD_OPTIONS_EXEMPT += foo.com
bin_SCRIPTS                        += foo.com
foo.com: foo-console$(EXEEXT)
        cp $^ $@

endif

List of changes:

  • Removed AUTOMAKE_OPTIONS = no-exeext which appears unnecessary.

  • Use CLEANFILES instead of hooking into clean-local.

  • Reformat for easy patching by adding/removing lines.

  • Add an AM_CONDITIONAL to build these windows specific programs if and only if you are actually building for a windows target. (For a MacOSX or GNU/Linux target, you would need to write and build very different programs.)

    # Add this to configure.ac
    build_for_windows=no
    case "${host_os}" in
         cygwin*|mingw*)
             build_for_windows=yes
             ;;
    esac
    AM_CONDITIONAL([BUILD_FOR_WINDOWS],
                   [test "x$build_for_windows" = "xyes"])
    
ndim
  • 35,870
  • 12
  • 47
  • 57