How can I make a target “private” in GNU make for internal use only? OR: how to best enforce target-specific variable-values?

后端 未结 5 1936
别那么骄傲
别那么骄傲 2021-02-19 13:14

I have some ancillary targets in a makefile that I want to restrict for internal or \"private\" use (only) inside the makefile. That is, I want to be able to specify these targ

5条回答
  •  暖寄归人
    2021-02-19 13:58

    I don't think there's any "elegant" way to have targets somehow made private. I think the only solution that could be called elegant would be to rewrite your makefile so that it doesn't matter what target users invoke, as Beta suggests. It would also have the advantage of making your makefile more maintainable and easier to understand.

    A not so elegant but fairly simple way to make targets "private" would be to rename the makefile to something other than one of the default names. Then put a new makefile in it's place that invokes the "private" makefile to do it's work. Something like:

    .SUFFIXES:
    
    PUBLIC_TARGETS = all debug release clean
    REAL_MAKEFILE = private.mak
    
    define invoke_make
     $(1): $(REAL_MAKEFILE)
        $(MAKE) -f $(REAL_MAKEFILE) $(1)
    endef
    
    $(foreach target, $(PUBLIC_TARGETS), $(eval $(call invoke_make,$(target))))
    
    .PHONY: $(PUBLIC_TARGETS)
    

    Obviously this doesn't prevent a determined user from invoking "private" targets, but hopefully it makes it clear that they shouldn't be doing this. That's all making things private in object-oriented languages does anyways. It's always possible for a sufficiently determined user to bypass it.

提交回复
热议问题