471 $(subst ?,$(SPACE),$(strip $1)) |
471 $(subst ?,$(SPACE),$(strip $1)) |
472 EncodeSpace = \ |
472 EncodeSpace = \ |
473 $(subst $(SPACE),?,$(strip $1)) |
473 $(subst $(SPACE),?,$(strip $1)) |
474 |
474 |
475 ################################################################################ |
475 ################################################################################ |
476 # Make directory without forking mkdir if not needed |
476 # Make directory without forking mkdir if not needed. |
|
477 # |
|
478 # If a directory with an encoded space is provided, the wildcard function |
|
479 # sometimes returns false answers (typically if the dir existed when the |
|
480 # makefile was parsed, but was deleted by a previous rule). In that case, always |
|
481 # call mkdir regardless of what wildcard says. |
|
482 # |
477 # 1: List of directories to create |
483 # 1: List of directories to create |
478 MakeDir = \ |
484 MakeDir = \ |
479 $(strip \ |
485 $(strip \ |
480 $(eval MakeDir_dirs_to_make := $(strip $(foreach d, $1, $(if $(wildcard $d), , \ |
486 $(eval MakeDir_dirs_to_make := $(strip $(foreach d, $1, \ |
481 "$(call DecodeSpace, $d)")))) \ |
487 $(if $(findstring ?, $d), '$(call DecodeSpace, $d)', \ |
|
488 $(if $(wildcard $d), , $d) \ |
|
489 ) \ |
|
490 ))) \ |
482 $(if $(MakeDir_dirs_to_make), $(shell $(MKDIR) -p $(MakeDir_dirs_to_make))) \ |
491 $(if $(MakeDir_dirs_to_make), $(shell $(MKDIR) -p $(MakeDir_dirs_to_make))) \ |
483 ) |
492 ) |
|
493 |
|
494 # Make directory for target file. Should handle spaces in filenames. Just |
|
495 # calling $(call MakeDir $(@D)) will not work if the directory contains a space |
|
496 # and the target file already exists. In that case, the target file will have |
|
497 # its wildcard ? resolved and the $(@D) will evaluate each space separated dir |
|
498 # part on its own. |
|
499 MakeTargetDir = \ |
|
500 $(call MakeDir, $(dir $(call EncodeSpace, $@))) |
484 |
501 |
485 ################################################################################ |
502 ################################################################################ |
486 # Assign a variable only if it is empty |
503 # Assign a variable only if it is empty |
487 # Param 1 - Variable to assign |
504 # Param 1 - Variable to assign |
488 # Param 2 - Value to assign |
505 # Param 2 - Value to assign |
497 # Cp has to operate in recursive mode to allow for -P flag, to preserve soft links. If the |
514 # Cp has to operate in recursive mode to allow for -P flag, to preserve soft links. If the |
498 # name of the target file differs from the source file, rename after copy. |
515 # name of the target file differs from the source file, rename after copy. |
499 # If the source and target parent directories are the same, recursive copy doesn't work |
516 # If the source and target parent directories are the same, recursive copy doesn't work |
500 # so we fall back on regular copy, which isn't preserving symlinks. |
517 # so we fall back on regular copy, which isn't preserving symlinks. |
501 define install-file |
518 define install-file |
502 $(call MakeDir, $(@D)) |
519 $(call MakeTargetDir) |
503 $(RM) '$(call DecodeSpace, $@)' |
520 $(RM) '$(call DecodeSpace, $@)' |
504 if [ '$(call DecodeSpace, $(dir $@))' != \ |
521 if [ '$(call DecodeSpace, $(dir $@))' != \ |
505 '$(call DecodeSpace, $(dir $(call EncodeSpace, $<)))' ]; then \ |
522 '$(call DecodeSpace, $(dir $(call EncodeSpace, $<)))' ]; then \ |
506 $(CP) -f -r -P '$(call DecodeSpace, $<)' '$(call DecodeSpace, $(@D))'; \ |
523 $(CP) -f -r -P '$(call DecodeSpace, $<)' '$(call DecodeSpace, $(@D))'; \ |
507 if [ '$(call DecodeSpace, $(@F))' != \ |
524 if [ '$(call DecodeSpace, $(@F))' != \ |
524 # attributes when they are present, failing on this is avoided. |
541 # attributes when they are present, failing on this is avoided. |
525 # |
542 # |
526 # If copying a soft link to a directory, need to delete the target first to avoid |
543 # If copying a soft link to a directory, need to delete the target first to avoid |
527 # weird errors. |
544 # weird errors. |
528 define install-file |
545 define install-file |
529 $(call MakeDir, $(@D)) |
546 $(call MakeTargetDir) |
530 $(RM) '$(call DecodeSpace, $@)' |
547 $(RM) '$(call DecodeSpace, $@)' |
531 $(CP) -fRP '$(call DecodeSpace, $<)' '$(call DecodeSpace, $@)' |
548 $(CP) -fRP '$(call DecodeSpace, $<)' '$(call DecodeSpace, $@)' |
532 if [ -n "`$(XATTR) -ls '$(call DecodeSpace, $@)'`" ]; then $(XATTR) -cs '$(call DecodeSpace, $@)'; fi |
549 if [ -n "`$(XATTR) -ls '$(call DecodeSpace, $@)'`" ]; then $(XATTR) -cs '$(call DecodeSpace, $@)'; fi |
533 endef |
550 endef |
534 else |
551 else |
535 define install-file |
552 define install-file |
536 $(call MakeDir, $(@D)) |
553 $(call MakeTargetDir) |
537 $(CP) -fP '$(call DecodeSpace, $<)' '$(call DecodeSpace, $@)' |
554 $(CP) -fP '$(call DecodeSpace, $<)' '$(call DecodeSpace, $@)' |
538 endef |
555 endef |
539 endif |
556 endif |
540 |
557 |
541 # Variant of install file that does not preserve symlinks |
558 # Variant of install file that does not preserve symlinks |
542 define install-file-nolink |
559 define install-file-nolink |
543 $(call MakeDir, $(@D)) |
560 $(call MakeTargetDir) |
544 $(CP) -f '$(call DecodeSpace, $<)' '$(call DecodeSpace, $@)' |
561 $(CP) -f '$(call DecodeSpace, $<)' '$(call DecodeSpace, $@)' |
545 endef |
562 endef |
546 |
563 |
547 ################################################################################ |
564 ################################################################################ |
548 # Take two paths and return the path of the last common directory. |
565 # Take two paths and return the path of the last common directory. |
588 # link-file-* works similarly to install-file but creates a symlink instead. |
605 # link-file-* works similarly to install-file but creates a symlink instead. |
589 # There are two versions, either creating a relative or an absolute link. Be |
606 # There are two versions, either creating a relative or an absolute link. Be |
590 # careful when using this on Windows since the symlink created is only valid in |
607 # careful when using this on Windows since the symlink created is only valid in |
591 # the unix emulation environment. |
608 # the unix emulation environment. |
592 define link-file-relative |
609 define link-file-relative |
593 $(call MakeDir, $(@D)) |
610 $(call MakeTargetDir) |
594 $(RM) '$(call DecodeSpace, $@)' |
611 $(RM) '$(call DecodeSpace, $@)' |
595 $(LN) -s '$(call DecodeSpace, $(call RelativePath, $<, $(@D)))' '$(call DecodeSpace, $@)' |
612 $(LN) -s '$(call DecodeSpace, $(call RelativePath, $<, $(@D)))' '$(call DecodeSpace, $@)' |
596 endef |
613 endef |
597 |
614 |
598 define link-file-absolute |
615 define link-file-absolute |
599 $(call MakeDir, $(@D)) |
616 $(call MakeTargetDir) |
600 $(RM) '$(call DecodeSpace, $@)' |
617 $(RM) '$(call DecodeSpace, $@)' |
601 $(LN) -s '$(call DecodeSpace, $<)' '$(call DecodeSpace, $@)' |
618 $(LN) -s '$(call DecodeSpace, $<)' '$(call DecodeSpace, $@)' |
602 endef |
619 endef |
603 |
620 |
604 ################################################################################ |
621 ################################################################################ |