DrylandEcology / STEPWAT2

folder
4 stars 5 forks source link

Decouple the Makefile from the SOILWAT2 file structure #483

Open chaukap opened 4 years ago

chaukap commented 4 years ago

Currently the STEPWAT2 makefile is tightly coupled to the file structure of SOILWAT2. there are 23 points of contact:

    sw_src/filefuncs.c 
    sw_src/generic.c 
    sw_src/mymemory.c 
    sw_src/pcg/pcg_basic.c 
    sw_src/rands.c 
    sw_src/SW_Carbon.c 
    sw_src/SW_Control.c 
    sw_src/SW_Files.c 
    sw_src/SW_Flow.c 
    sw_src/SW_Flow_lib.c 
    sw_src/SW_Markov.c 
    sw_src/SW_Model.c 
    sw_src/SW_Output.c 
    sw_src/SW_Output_get_functions.c 
    sw_src/SW_Output_outarray.c 
    sw_src/SW_Output_outtext.c 
    sw_src/SW_Site.c 
    sw_src/SW_Sky.c 
    sw_src/SW_SoilWater.c 
    sw_src/SW_VegEstab.c 
    sw_src/SW_VegProd.c 
    sw_src/SW_Weather.c 
    sw_src/Times.c 

If any one of these files is moved STEPWAT2 will not compile.

Solution

We can reduce the points of contact to 2:

sw_src/makefile
sw_src/libSOILWAT2.a

SOILWAT2's makefile has a rule to create a library file. As long as we know where the makefile and the library file are located the rest of SOILWAT2 can be abstracted away.

However, there are a few caveats. Variables can no longer be externed across the two codebases. This means variables like SXW and SuperGlobals must be declared in header files. This should be fairly easy to do.

Unfortunately, the other change must be done on the SOILWAT2 side. There are 2 source files that are currently not compiled into the SOILWAT2 library: SW_Output_outtext.c and SW_Output_outarray.c. These must be added to the sources_lib variable in the SOILWAT2 makefile.

chaukap commented 4 years ago

To elaborate on the externed variables issue, we will need to move the declaration of the following variables:

From ST_main.c to ST_globals.h:

Technically we only need to move SuperGlobals and Globals, but while we're at it we might as well move the rest of them for the sake of clean code.

From sxw.c to sxw.h:

As a side effect of moving SXW to sxw.h the file sxw_vars.h will be rendered completely useless. It should probably be removed.

dschlaep commented 4 years ago

Re:

Unfortunately, the other change must be done on the SOILWAT2 side. There are 2 source files that are currently not compiled into the SOILWAT2 library: SW_Output_outtext.c and SW_Output_outarray.c. These must be added to the sources_lib variable in the SOILWAT2 makefile.

--> The SOILWAT2 makefile accounts for this issue because the output needs are so different for SOILWAT2-standalone, rSOILWAT2, and STEPWAT2: the makefile accepts sw_sources to add additional source files to be compiled into the SOILWAT2 library. See the example in rSOILWAT2 https://github.com/DrylandEcology/rSOILWAT2/blob/master/src/Makevars

...
sw_sources = SW_Output_outarray.c
...
$(path_target)/$(lib_target):
    @(cd $(path_target) && $(MAKE) $(lib_target) \
        CC="$(CC)" CPPFLAGS="$(ALL_CPPFLAGS)" CFLAGS="$(ALL_CFLAGS)" AR="$(AR)" \
        sw_sources="$(sw_sources)")
...

so for STEPWAT2, this could then be adjusted to something like

sw_sources = SW_Output_outtext.c SW_Output_outarray.c
chaukap commented 4 years ago

@dschlaep Thanks for the help! This should make the implementation very easy for the person that implements it.