How do I define Makefile variables from a Perl script?

I am creating a Makefile which I want to be a single file for different architectures, operating systems, libraries, etc. To do this, I have an assembly-specific XML file that defines different configuration options for each architecture. The configuration file is read by Perl (it can be any language), and the Perl output returns something like:

var1 := var1_value
var2 := var2_value
var3 := var3_value

      

What I am trying to do is define these variables in my Makefile. From the makefile, I call my readconfig script and it gives the correct output, but I was unable to get these variables as part of my Makefile. I tried using eval

and value

, but none of them worked (although it might be a problem for me not knowing how to use them. Overall what I'm trying to do is something like:

read_config:
     $(eval (perl '-require "readConfig.pl"'))
     @echo $(var1)

      

It can be assumed that I am only using the GNU Make behavior. Things I couldn't change:

  • The config file is in XML
  • Using Perl as an XML Parser
+2


a source to share


3 answers


You don't need to have config.mk as an explicit target; if you include it, Make will do (if necessary):



-include config.mk

all: real_targets

config.mk: xml_file
    perl make-config.pl > $@

      

+3


a source


I think the directive you are looking for is "include":

include config.mk

...rest of makefile...

      

Your script creates a file config.mk

; makefile reads it. If you need the makefile to run the generator, it gets more complex:



MAKEFILE_INCLUDE = dummy.mk
include ${MAKEFILE_INCLUDE}

all: normal dependencies for target

config: config.mk
        perl make-config.pl > config.mk
        ${MAKE} MAKEFILE_INCLUDE=config.mk

      

First you run make config

(only with an empty or nearly empty file dummy.mk

). Then it will run make. It's easier if you don't try it; goals other than all

becoming complex, etc. There are ways, but they are increasingly distorted.

+5


a source


A version that makes the configuration part of the default all target.

-include config.mk

all: config.mk and other targets

config.mk:
        perl make-config.pl >config.mk

      

+1


a source







All Articles