]> rtime.felk.cvut.cz Git - omk.git/commitdiff
Simplify program compilation
authorMichal Sojka <sojkam1@fel.cvut.cz>
Tue, 12 Nov 2013 15:27:33 +0000 (16:27 +0100)
committerMichal Sojka <sojkam1@fel.cvut.cz>
Tue, 12 Nov 2013 16:12:18 +0000 (17:12 +0100)
Now, PROGRAM_template does not use global variables and rule generation
($(eval $(call ...))) is not split over multiple places. Now, we simply
call $(eval $(call PROGRAM_template,...)) and all rules for program
compilation are generated.

snippets/linux.omk

index a9635f5d544f81bfd953527942930a6d4b0416c4..a7a3b70879d4d4400d96bc1ddffa206aee11e135 100644 (file)
@@ -102,8 +102,6 @@ endif
 
 ifdef USER_RULE_TEMPLATES
 
-USER_SOURCES2OBJS = .o/.c .o/.cc .o/.cxx .o/.cpp .o/.S .o/.o
-
 USER_SOURCES2OBJSLO = .lo/.c .lo/.cc .lo/.cxx .lo/.cpp .lo/.S .lo/.lo
 
 #%.lo: %.c
@@ -134,34 +132,43 @@ endif
 
 # Syntax: $(call COMPILE_c_o_template,<source>,<target>,<additional c-flags>)
 define COMPILE_c_o_template
+ifeq ($$($(2)_C_TARGET),)
+$(2)_C_TARGET=1
 $(2): $(1) $$(GEN_HEADERS)
        @$(QUIET_CMD_ECHO) "  CC      $$@"
        $(Q) if $$(c_o_COMPILE) $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \
        then mv -f "$$@.d.tmp" "$$@.d" ; \
        else rm -f "$$@.d.tmp" ; exit 1; \
        fi
+endif
 endef
 
 
 # Syntax: $(call COMPILE_cc_o_template,<source>,<target>,<additional c-flags>)
 define COMPILE_cc_o_template
+ifeq ($$($(2)_CC_TARGET),)
+$(2)_CC_TARGET=1
 $(2): $(1) $$(GEN_HEADERS)
        @$(QUIET_CMD_ECHO) "  CXX     $$@"
        $(Q) if $$(cc_o_COMPILE) $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \
        then mv -f "$$@.d.tmp" "$$@.d" ; \
        else rm -f "$$@.d.tmp" ; exit 1; \
        fi
+endif
 endef
 
 
 # Syntax: $(call COMPILE_S_o_template,<source>,<target>,<additional c-flags>)
 define COMPILE_S_o_template
+ifeq ($$($(2)_S_TARGET),)
+$(2)_S_TARGET=1
 $(2): $(1) $$(GEN_HEADERS)
        @$(QUIET_CMD_ECHO) "  AS      $$@"
        $(Q) if $$(S_o_COMPILE) -D__ASSEMBLY__ $$(CC_DEPFLAGS) $(3) -o $$@ -c $$< ; \
        then mv -f "$$@.d.tmp" "$$@.d" ; \
        else rm -f "$$@.d.tmp" ; exit 1; \
        fi
+endif
 endef
 
 NM ?= nm
@@ -194,36 +201,47 @@ endef
 
 
 define COMPILE_idl_template
+GEN_HEADERS+=$(filter %.h,$(1:%.idl=%.h))
+
 $(2).c $(2)-stubs.c $(2)-skels.c $(2)-common.c $(2).h: $(1) $$(wildcard $$(firstword $$(idl_COMPILE)))
        @$(QUIET_CMD_ECHO) "  IDL     $$@"
        $(Q) $$(idl_COMPILE) $$($(2)_IDLFLAGS) $(1)
 endef
 
+# Syntax: $(call COMPILE_templates,<sources>)
+# Note: The newlines after $(call ) are IMPORTANT!!!
+define COMPILE_templates
+$(foreach src,$(filter %.c,$(1)),$(call COMPILE_c_o_template,$(SOURCES_DIR)/$(src),$(src:%.c=%.o),)
+)
+$(foreach src,$(filter %.cc,$(1)),$(call COMPILE_cc_o_template,$(SOURCES_DIR)/$(src),$(src:%.cc=%.o),)
+)
+$(foreach src,$(filter %.cxx,$(1)),$(call COMPILE_cc_o_template,$(SOURCES_DIR)/$(src),$(src:%.cxx=%.o),)
+)
+$(foreach src,$(filter %.cpp,$(1)),$(call COMPILE_cc_o_template,$(SOURCES_DIR)/$(src),$(src:%.cpp=%.o),)
+)
+$(foreach src,$(filter %.S,$(1)),$(call COMPILE_S_o_template,$(SOURCES_DIR)/$(src),$(src:%.S=%.o),)
+)
+endef
+
+TARGET_GEN_SOURCES =  $(filter %.c,$($(1)_SERVER_IDL:%.idl=%-skels.c)) \
+                      $(filter %.c,$($(1)_SERVER_IDL:%.idl=%-common.c)) \
+                      $(filter %.c,$($(1)_CLIENT_IDL:%.idl=%-stubs.c)) \
+                      $(filter %.c,$($(1)_CLIENT_IDL:%.idl=%-common.c)) \
+                      $(filter %.c,$($(1)_IDL:%.idl=%.c))
+TARGET_SOURCES = $($(1)_SOURCES) $(TARGET_GEN_SOURCES)
+TARGET_IDLS = $($(1)_SERVER_IDL) $($(1)_CLIENT_IDL) $($(1)_IDL)
+LINK_WITH_CXX = $(filter %.cc,$(TARGET_SOURCES))$(filter %.cxx,$(TARGET_SOURCES))$$(filter %.cpp,$(TARGET_SOURCES))
 
 # Syntax: $(call PROGRAM_template,<executable-name>,<dir>,<executable-suffix>,<linker-sript>)
 define PROGRAM_template
+$(foreach idl,$(TARGET_IDLS),$(call COMPILE_idl_template,$(SOURCES_DIR)/$(idl),$(idl:%.idl=%)))
 
-USER_IDLS  += $$($(1)_SERVER_IDL) $$($(1)_CLIENT_IDL) $$($(1)_IDL)
-$(1)_GEN_SOURCES += $$(filter %.c,$$($(1)_SERVER_IDL:%.idl=%-skels.c))
-$(1)_GEN_SOURCES += $$(filter %.c,$$($(1)_SERVER_IDL:%.idl=%-common.c))
-$(1)_GEN_SOURCES += $$(filter %.c,$$($(1)_CLIENT_IDL:%.idl=%-stubs.c))
-$(1)_GEN_SOURCES += $$(filter %.c,$$($(1)_CLIENT_IDL:%.idl=%-common.c))
-$(1)_GEN_SOURCES += $$(filter %.c,$$($(1)_IDL:%.idl=%.c))
-USER_GEN_SOURCES += $$($(1)_GEN_SOURCES)
-
-$(foreach x, $(USER_SOURCES2OBJS),
-$(1)_OBJS += $$(patsubst %$(notdir $(x)),%$(dir $(x)),$$(filter %$(notdir $(x)),\
-               $$($(1)_SOURCES) $$($(1)_GEN_SOURCES)))
-)
-$(1)_OBJS := $$(sort $$($(1)_OBJS:%/=%))
-
-USER_OBJS  += $$($(1)_OBJS)
-USER_SOURCES += $$($(1)_SOURCES)
+$(call COMPILE_templates,$(TARGET_SOURCES))
 
-$(2)/$(1)$(3): $$($(1)_OBJS)
+$(2)/$(1)$(3): $(sort $(addsuffix .o,$(basename $(TARGET_SOURCES))))
        @$(QUIET_CMD_ECHO) "  LINK    $$@"
-       $(Q) $$(if $$(filter %.cc,$$($(1)_SOURCES))$$(filter %.cxx,$$($(1)_SOURCES))$$(filter %.cpp,$$($(1)_SOURCES)),$$(CXX),$$(CC)) \
-         $$($(1)_OBJS) $$($(1)_LIBS:%=-l%) $$(LOADLIBES) $$(LDFLAGS) $$($(1)_LDFLAGS) -Wl,-rpath-link,$(USER_LIB_DIR) -Wl,-Map,$(USER_OBJS_DIR)/$(1).exe.map -o $$@
+       $(Q) $(if $(LINK_WITH_CXX),$$(CXX),$$(CC)) \
+         $$^ $$($(1)_LIBS:%=-l%) $$(LOADLIBES) $$(AM_LDFLAGS) $$(LDFLAGS) $$($(1)_LDFLAGS) -Wl,-rpath-link,$(USER_LIB_DIR) -Wl,-Map,$(USER_OBJS_DIR)/$(1).exe.map -o $$@
        @echo "$(2)/$(1)$(3): \\" >$(USER_OBJS_DIR)/$(1).exe.d
        @$(SED4OMK) -n -e 's|^LOAD \(.*\)$$$$|  \1  \&|p' $(USER_OBJS_DIR)/$(1).exe.map|tr '&' '\134'  >>$(USER_OBJS_DIR)/$(1).exe.d
        @echo >>$(USER_OBJS_DIR)/$(1).exe.d
@@ -254,9 +272,6 @@ $(1)_OBJS += $$(patsubst %$(notdir $(x)),%$(dir $(x)),$$(filter %$(notdir $(x)),
 )
 $(1)_OBJS := $$(sort $$($(1)_OBJS:%/=%))
 
-USER_OBJS  += $$($(1)_OBJS)
-USER_SOURCES += $$($(1)_SOURCES)
-
 $(USER_LIB_DIR)/lib$(1).a: $$($(1)_OBJS)
        @$(QUIET_CMD_ECHO) "  AR      $$@"
        $(Q) $(AR) rcs $$@ $$^
@@ -273,7 +288,7 @@ $(1)_GEN_SOURCES += $$(filter %.c,$$($(1)_SERVER_IDL:%.idl=%-common.c))
 $(1)_GEN_SOURCES += $$(filter %.c,$$($(1)_CLIENT_IDL:%.idl=%-stubs.c))
 $(1)_GEN_SOURCES += $$(filter %.c,$$($(1)_CLIENT_IDL:%.idl=%-common.c))
 $(1)_GEN_SOURCES += $$(filter %.c,$$($(1)_IDL:%.idl=%.c))
-SOLIB_GEN_SOURCES += $$($(1)_GEN_SOURCES)
+SOLIB_GEN_SOURCES += $
 
 $(foreach x, $(USER_SOURCES2OBJSLO),
 $(1)_OBJSLO += $$(patsubst %$(notdir $(x)),%$(dir $(x)),$$(filter %$(notdir $(x)),\
@@ -282,7 +297,9 @@ $(1)_OBJSLO += $$(patsubst %$(notdir $(x)),%$(dir $(x)),$$(filter %$(notdir $(x)
 $(1)_OBJSLO := $$(sort $$($(1)_OBJSLO:%/=%))
 
 SOLIB_OBJS  += $$($(1)_OBJSLO)
-SOLIB_SOURCES += $$($(1)_SOURCES)
+SOLIB_SOURCES += $
+
+$(call COMPILE_templates,$($(1)_SOURCES) $($(1)_GEN_SOURCES))
 
 $(OMK_WORK_DIR)/lib$(1).$(SOLIB_EXT).omkvar: $$($(1)_OBJSLO) FORCE
        $(Q)echo '$(1)_objslo += $$$$(addprefix $(USER_OBJS_DIR)/,$$($(1)_OBJSLO))' > $$@.tmp; \
@@ -311,8 +328,6 @@ $(foreach cmetrh,$(cmetric_include_HEADERS),$(eval $(call CMETRIC_o_h_template,\
 
 GEN_HEADERS+=$(cmetric_include_HEADERS:%=$(USER_INCLUDE_DIR)/%)
 
-GEN_HEADERS+=$(filter %.h,$(USER_IDLS:%.idl=%.h))
-
 # Generate rules for compilation of programs and libraries
 
 $(foreach prog,$(utils_PROGRAMS),$(eval $(call PROGRAM_template,$(prog),$(USER_UTILS_DIR),$(EXE_SUFFIX))))
@@ -379,35 +394,6 @@ $(eval $(call include-pass-template,$(USER_INCLUDE_DIR),include))
 
 ifdef USER_RULE_TEMPLATES
 
-# User-space static libraries and applications object files
-
-USER_SOURCES := $(sort $(USER_SOURCES))
-
-USER_GEN_SOURCES := $(sort $(USER_GEN_SOURCES))
-
-#$(warning USER_SOURCES = $(USER_SOURCES))
-
-$(foreach src,$(filter %.c,$(USER_SOURCES)),$(eval $(call COMPILE_c_o_template,$(SOURCES_DIR)/$(src),$(src:%.c=%.o),)))
-
-$(foreach src,$(filter %.cc,$(USER_SOURCES)),$(eval $(call COMPILE_cc_o_template,$(SOURCES_DIR)/$(src),$(src:%.cc=%.o),)))
-
-$(foreach src,$(filter %.cxx,$(USER_SOURCES)),$(eval $(call COMPILE_cc_o_template,$(SOURCES_DIR)/$(src),$(src:%.cxx=%.o),)))
-
-$(foreach src,$(filter %.cpp,$(USER_SOURCES)),$(eval $(call COMPILE_cc_o_template,$(SOURCES_DIR)/$(src),$(src:%.cpp=%.o),)))
-
-$(foreach src,$(filter %.S,$(USER_SOURCES)),$(eval $(call COMPILE_S_o_template,$(SOURCES_DIR)/$(src),$(src:%.S=%.o),)))
-
-$(foreach src,$(filter %.c,$(USER_GEN_SOURCES)),$(eval $(call COMPILE_c_o_template,$(src),$(src:%.c=%.o),)))
-
-# User-space shared libraries object files
-
-SOLIB_SOURCES := $(sort $(SOLIB_SOURCES))
-
-SOLIB_GEN_SOURCES := $(sort $(SOLIB_GEN_SOURCES))
-
-#$(warning SOLIB_SOURCES = $(SOLIB_SOURCES))
-#$(warning SOLIB_GEN_SOURCES = $(SOLIB_GEN_SOURCES))
-
 $(foreach src,$(filter %.c,$(SOLIB_SOURCES)),$(eval $(call COMPILE_c_o_template,$(SOURCES_DIR)/$(src),$(src:%.c=%.lo),$(SOLIB_PICFLAGS))))
 
 $(foreach src,$(filter %.cc,$(SOLIB_SOURCES)),$(eval $(call COMPILE_cc_o_template,$(SOURCES_DIR)/$(src),$(src:%.cc=%.lo),$(SOLIB_PICFLAGS))))
@@ -426,12 +412,6 @@ ifneq ($(_dirs_to_create),)
 $(shell mkdir -p $(addprefix $(LOCAL_BUILD_DIR)/,$(_dirs_to_create)))
 endif
 
-# IDL compilation
-
-USER_IDLS := $(sort $(USER_IDLS))
-
-$(foreach src,$(filter %.idl,$(USER_IDLS)),$(eval $(call COMPILE_idl_template,$(SOURCES_DIR)/$(src),$(src:%.idl=%))))
-
 endif
 
 clean-local::