Automatic recompile when flags change

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Automatic recompile when flags change

Pauli .
Hello

In many cases partial build with differing flags can results to hard
to debug runtime bugs. To avoid issues when compiler flags change
build system could automatically rebuild files when flags change. But
so far automake doesn't support flag change checking

I decided to try to implement checking my self. Following
implementation seems fairly simple but I think implementation depends
on gnu make specific features. It supports only c++ and seperation of
language specific flags is missing (to recompile only C files if
CFLAGS changes).

I'm wondering if this checking could be implemented in automake. I
don't want to attempt to implement this my self because I don't have
experience writing portable makefiles.

# Add compiler flags dependency to targets
flags_verbose = $(flags_verbose_@AM_V@)
flags_verbose_ = $(flags_verbose_@AM_DEFAULT_V@)
flags_verbose_0 = @echo "  CHK-FLAG $@";
flags_verbose_1 =

#Helper for variable lists
varprefix=$(foreach V,$(2),$($(1)$(V)))

FD_VARS = CPPFLAGS CXXFLAGS LDFLAGS LDADD

FD_GLOBALS = $(call varprefix,AM_,$(FD_VARS)) $(call varprefix,,$(FD_VARS))

%.flags: %.tflags
        $(flags_verbose)if ! cmp -s $< $@; then cp $< $@; fi; $(RM) $<

# Generate rules and dependencies for a target
# param 1: target filename
# param 2: target variable prefix
define flagsdep_single

$(1).tflags:
        @echo "$$(FD_GLOBALS) $$(call varprefix,$(2)_,$$(FD_VARS))" > $$@;

$$(am_$(2)_OBJECTS): $(1).flags

$(1): $(1).flags

endef

# Callable variable to add flag dependency to targets
flagsdep=$(foreach T,$(1),$(call flagsdep_single,$(T),$(subst
.,_,$(subst $(EXEEXT),,$(T)))))

$(eval $(call flagsdep,$(LTLIBRARIES) $(PROGRAMS) $(check_PROGRAMS)))

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Automatic recompile when flags change

Basin Ilya
Hi.
I think $(eval) is less portable than $(shell)

Instead of injecting a dependency to all targets you can generate
config.h with autoconf and include it at the beginning of all source
files. Automake plus gcc will make sure that the object files depend on it.

If you change your CFLAGS by running ./configure, you can save them to
config.h. If you plan to pass CFLAGS as make command line arguments,
then you must add a dependency to config.h:

    $(top_srcdir)/config.h: $(shell
$(top_srcdir)/build-aux/check-cflags.sh $(CFLAGS))

    .PHONY: cflags-changed
    cflags-changed: # do nothing

The script ./build-aux/check-cflags.sh will check CFLAGS and if they
change, save them to a file for later comparison and print the PHONY
target name "cflags-changed".

You must also touch config.h in the script before saving CFLAGS in case
make is interrupted.


On 20.10.2016 14:26, Pauli . wrote:

> Hello
>
> In many cases partial build with differing flags can results to hard
> to debug runtime bugs. To avoid issues when compiler flags change
> build system could automatically rebuild files when flags change. But
> so far automake doesn't support flag change checking
>
> I decided to try to implement checking my self. Following
> implementation seems fairly simple but I think implementation depends
> on gnu make specific features. It supports only c++ and seperation of
> language specific flags is missing (to recompile only C files if
> CFLAGS changes).
>
> I'm wondering if this checking could be implemented in automake. I
> don't want to attempt to implement this my self because I don't have
> experience writing portable makefiles.
>
> # Add compiler flags dependency to targets
> flags_verbose = $(flags_verbose_@AM_V@)
> flags_verbose_ = $(flags_verbose_@AM_DEFAULT_V@)
> flags_verbose_0 = @echo "  CHK-FLAG $@";
> flags_verbose_1 =
>
> #Helper for variable lists
> varprefix=$(foreach V,$(2),$($(1)$(V)))
>
> FD_VARS = CPPFLAGS CXXFLAGS LDFLAGS LDADD
>
> FD_GLOBALS = $(call varprefix,AM_,$(FD_VARS)) $(call varprefix,,$(FD_VARS))
>
> %.flags: %.tflags
>         $(flags_verbose)if ! cmp -s $< $@; then cp $< $@; fi; $(RM) $<
>
> # Generate rules and dependencies for a target
> # param 1: target filename
> # param 2: target variable prefix
> define flagsdep_single
>
> $(1).tflags:
>         @echo "$$(FD_GLOBALS) $$(call varprefix,$(2)_,$$(FD_VARS))" > $$@;
>
> $$(am_$(2)_OBJECTS): $(1).flags
>
> $(1): $(1).flags
>
> endef
>
> # Callable variable to add flag dependency to targets
> flagsdep=$(foreach T,$(1),$(call flagsdep_single,$(T),$(subst
> .,_,$(subst $(EXEEXT),,$(T)))))
>
> $(eval $(call flagsdep,$(LTLIBRARIES) $(PROGRAMS) $(check_PROGRAMS)))
>

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Automatic recompile when flags change

Bob Friesenhahn
In reply to this post by Pauli .
On Thu, 20 Oct 2016, Pauli . wrote:
>
> In many cases partial build with differing flags can results to hard
> to debug runtime bugs. To avoid issues when compiler flags change
> build system could automatically rebuild files when flags change. But
> so far automake doesn't support flag change checking

Why do compiler flags change?  Did you re-run configure with different
CPPFLAGS/CFLAGS or are these flags induced on the make command line or
via environment variables?

Bob
--
Bob Friesenhahn
[hidden email], http://www.simplesystems.org/users/bfriesen/
GraphicsMagick Maintainer,    http://www.GraphicsMagick.org/

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Automatic recompile when flags change

Pauli .
On Thu, Oct 20, 2016 at 8:33 PM, Bob Friesenhahn
<[hidden email]> wrote:

> On Thu, 20 Oct 2016, Pauli . wrote:
>>
>>
>> In many cases partial build with differing flags can results to hard
>> to debug runtime bugs. To avoid issues when compiler flags change
>> build system could automatically rebuild files when flags change. But
>> so far automake doesn't support flag change checking
>
>
> Why do compiler flags change?  Did you re-run configure with different
> CPPFLAGS/CFLAGS or are these flags induced on the make command line or via
> environment variables?
>

It can happen for many reason but most likely to cause trouble for
developer is when configure.ac changes which leads to automatic
reconfigure with different flags.

Pauli

Loading...