Skip to main content
added 43 characters in body
Source Link
Gilles 'SO- stop being evil'
  • 865.5k
  • 205
  • 1.8k
  • 2.3k

Do you mean the first component of the directory? If so, turn the path into a list of components, then extract the first component. I assume you're using GNU make, and you're not doing something insane like expect file names with whitespace to go through unscathed. Don't try this with an absolute path.

override BUILD_DIR = dir/subdir/.../sub-a-dub-dir
distclean:
        rm -rf $(firstword $(subst /, ,$(BUILD_DIR)))

(This looks quite dangerous. What if someone wants to set an alternate build directory and runs I make make BUILD_DIR=$PWD/my/build/dirBUILD_DIR?) override whatever the user might have passed on the command line, to avoid someone accidentally passing it and having something unintended deleted.

Do you mean the first component of the directory? If so, turn the path into a list of components, then extract the first component. I assume you're using GNU make, and you're not doing something insane like expect file names with whitespace to go through unscathed. Don't try this with an absolute path.

BUILD_DIR = dir/subdir/.../sub-a-dub-dir
distclean:
        rm -rf $(firstword $(subst /, ,$(BUILD_DIR)))

(This looks quite dangerous. What if someone wants to set an alternate build directory and runs make BUILD_DIR=$PWD/my/build/dir?)

Do you mean the first component of the directory? If so, turn the path into a list of components, then extract the first component. I assume you're using GNU make, and you're not doing something insane like expect file names with whitespace to go through unscathed. Don't try this with an absolute path.

override BUILD_DIR = dir/subdir/.../sub-a-dub-dir
distclean:
        rm -rf $(firstword $(subst /, ,$(BUILD_DIR)))

I make BUILD_DIR override whatever the user might have passed on the command line, to avoid someone accidentally passing it and having something unintended deleted.

Source Link
Gilles 'SO- stop being evil'
  • 865.5k
  • 205
  • 1.8k
  • 2.3k

Do you mean the first component of the directory? If so, turn the path into a list of components, then extract the first component. I assume you're using GNU make, and you're not doing something insane like expect file names with whitespace to go through unscathed. Don't try this with an absolute path.

BUILD_DIR = dir/subdir/.../sub-a-dub-dir
distclean:
        rm -rf $(firstword $(subst /, ,$(BUILD_DIR)))

(This looks quite dangerous. What if someone wants to set an alternate build directory and runs make BUILD_DIR=$PWD/my/build/dir?)