Skip to main content
2 of 2
added 43 characters in body
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)))

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.

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