help-make
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Implement Check on Input Values


From: Lane Schwartz
Subject: Re: Implement Check on Input Values
Date: Mon, 8 Aug 2011 14:10:55 -0400

On Mon, Aug 8, 2011 at 12:13 PM, Ajay Jain <address@hidden> wrote:
> Hi,
>
> In my make system, I need to implement check on the value of some
> input parameters. For example, my makefile excepts WORK_DIR from
> environment and checks the following:
>
> 1) Check to make sure that WORK_DIR is not NULL
> 2) Check to make sure that WORK_DIR exists
> 3) Check to make sure that WORK_DIR is NOT a part of TOP_DIR/Parent Directory
>
> I need to check this logic within a rule and not otherwise as this is
> not applicable to all targets.
>
> So I wrote a (dirty) logic as follows:
>
> check_work_dir :
>    $(SET) -ex ; \
> if [ "$(WORK_DIR)" = "" ]; \
> then $(ECHO) "WORK_DIR is NULL" ; \
> exit -1; \
> fi; \
> if [ ! -d "$(WORK_DIR)" ]; \
> then $(ECHO) "$(WORK_DIR) does NOT exist .." ;\
> exit -1;\
> fi; \
> cd "$(WORK_DIR)"; \
> tmp_dir1=`pwd`; \
> tmp_dir2=$(dir $(dir $(TOP_DIR))); \
> $(ECHO) "$$tmp_dir1" \| grep "$$tmp_dir2" ; \
> if [ $$? -eq 0 ]; \
> then $(ECHO) "WORK_DIR : $(WORK_DIR) should NOT be contained inside
> $$tmp_dir2"; \
> exit -1; \
> fi
>
> Is there a better way to implement this logic?

Yes, there is, using various built-in make functions.

1) You can use ifndef or $(if ) to check for non-null.

2) You can use $(if ) in conjunction with $(wildcard ) to check for
directory existence.

3) For your third check, you should be able to use a combination of
ifeq and $(dir ).

Then, use $(error ) or $(warning ) to report when a problematic
condition is detected. Things should look something like the code
below (I haven't actually run the code below):

# Check condition 1
ifndef WORK_DIR
$(error The required variable WORK_DIR is not defined)
endif

# Check condition 2
$(if $(wildcard ${WORK_DIR}),,$(error The directory specified by
WORK_DIR does not exist))

# Check condition 3
ifeq (${WORK_DIR},$(dir $(dir ${TOP_DIR}))/$(notdir ${WORK_DIR}))
$(error The specified WORK_DIR is located in $(dir $(dir ${TOP_DIR})),
which is not allowed)
endif


Now, in the example make file that you gave, you had your checking
inside a make target. The code that I listed can stand independently
(that is, not inside a make target). Just put it at the top of your
file, or wherever is convenient.

Cheers,
Lane



reply via email to

[Prev in Thread] Current Thread [Next in Thread]