Subject: bug#78975: Feature request: per-library compiler

From: William T Jones <[email-masked]>
Date: Tue, 8 Jul 2025 21:53:11 -0400

I would like to request a feature for GNU Automake if it is possible.

When building programs and libraries with GNU Autotools (libtool, 
aclocal, automake, and autoconf) it is sometimes desirable to compile 
the same source with a custom compiler (other than CC, CXX, etc.).  A 
sample use case is to compile the same source file for multiple device 
accelerator architectures (CUDA, HIP, CPU, etc.) into a specific program 
or library.

In this use case, the same source modules need to be compile and linked 
into a device specific program or library.  Since the same source would 
be compiled for multiple devices, it would be advantageous to compile 
the source with multiple compilers within a single Makefile.am.  The 
desire would be to specify the compiler on a per-program (or 
per-library) basis in keeping with the ability to specify program and 
library variables (`maude_CFLAGS', `maude_LINK', etc.).  The suggestion 
would be to add new variables (`maude_CC', `maude_CXX', etc.) and use 
those in the custom object targets (`maude-%.o: %.c' where the stem is 
expanded) in place of $(CC), $(CXX), etc..

An example Makefile.am would contain the following:

lib_LTLIBRARIES = libcpu.la libcuda.la libhip.la

my_SRCS = source.c

libcpu_la_SOURCES = $(my_SRCS)
libcpu_la_CFLAGS =

libcuda_la_SOURCES = $(my_SRCS)
libcuda_la_CFLAGS = -Wc,-x,cu -Wc,-dc
libcuda_la_CC = nvcc

libhip_la_SOURCES = $(my_SRCS)
libhip_la_CFLAGS = -Wc,-fgpu-rdc -Wc,-dc
libhip_la_LDFLAGS = -Wc,-fgpu-rdc,--hip-link
libhip_la_CC = hipcc

And, as an example, the generated rule for compiling source.c for 
libcuda.la would be generated as ($CC replaced with $libcuda_la_CC):

libcuda_la-source.lo: source.c
         $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) 
$(LIBTOOLFLAGS) --mode=compile $(libcuda_la_CC) $(DEFS) 
$(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) 
$(libcuda_la_CFLAGS) $(CFLAGS) -MT libcuda_la-source.lo -MD -MP -MF 
$(DEPDIR)/libcuda_la-source.Tpo -c -o libcuda_la-source.lo `test -f 
'source.c' || echo '$(srcdir)/'`source.c
         $(AM_V_at)$(am__mv) $(DEPDIR)/libcuda_la-source.Tpo 
$(DEPDIR)/libcuda_la-source.Plo
#       $(AM_V_CC)source='source.c' object='libcuda_la-source.lo' 
libtool=yes \
#       DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) \
#       $(AM_V_CC_no)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) 
$(LIBTOOLFLAGS) --mode=compile $(libcuda_la_CC) $(DEFS) 
$(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) 
$(libcuda_la_CFLAGS) $(CFLAGS) -c -o libcuda_la-source.lo `test -f 
'source.c' || echo '$(srcdir)/'`source.c

Is this possible and acceptable?

If there is another way to accomplish this task, I am open to it. 
However, I have researched the topic with documentation searches and 
failed AI chats and have not discovered an elegant solution.

Thank you for all of the great work,

Bill

--