Search code examples
cassemblyautotoolsautoconfautomake

Using Autotools for a project with platform specific source code


I'm developing a project that is currently written in C but I plan to write some of the functions in ASM for at least two platforms (x86_64 and arm). So I might have some source files:

  • generic/one.c
  • generic/two.c
  • generic/three.c
  • arm/one.s
  • x86_64/two.s

I'd like it so that the configure script chooses the .s files over the .c files when possible. So building on arm will be one.s, two.c, three.c etc.

It seems difficult or impossible to do this nicely with Automake. But if I ditch Automake I'll have to track my own dependencies (ugh).

What's the best way to do this?


Solution

  • The Conditional Sources section of the automake manual should point you in the right direction. You're going to want AC_CANONICAL_HOST in configure.ac and then make decisions based on the value of $host.

    automake won't like it if two files can compile to the same object. If you can't rename the sources like in the linked part of the manual, you may want to try something like this (make sure you have subdir-objects in AM_INIT_AUTOMAKE:

    # Note the $() isn't a shell command substitution.
    # The quoting means it'll be expanded by `make'.
    MODULE_ONE_OBJ='generic/one.$(OBJEXT)'
    MODULE_TWO_OBJ='generic/two.$(OBJEXT)'
    case $host in
      # ...
      arm*)
        MODULE_ONE='arm/one.$(OBJEXT)'
        ;;
      x86_64)
        MODULE_TWO='x86/two.$(OBJEXT)'
        ;;
    esac
    AC_SUBST([MODULE_ONE])
    AC_SUBST([MODULE_TWO])
    

    In Makefile.am:

    bin_PROGRAMS = foo
    foo_SOURCES = foo.c
    EXTRA_foo_SOURCES = arm/one.s x86/two.c
    foo_LDADD = $(MODULE_ONE) $(MODULE_TWO)
    foo_DEPENDENCIES = $(MODULE_ONE) $(MODULE_TWO)