# Custom Target Source Script

**URL:** https://discourse.cmake.org/t/custom-target-source-script/4775
**Category:** Usage
**Tags:** os:linux
**Created:** [January 4, 2022, 9:31pm UTC](https://discourse.cmake.org/t/custom-target-source-script/4775 "2022-01-04T21:31:52Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![developer](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/d/bcef8e/32.png) [@developer](https://discourse.cmake.org/u/developer)
#### Post date: [January 4, 2022, 9:31pm UTC](https://discourse.cmake.org/t/custom-target-source-script/4775/1 "2022-01-04T21:31:52Z")

</div>

I would like to make a custom target which could build a local existing make based project. Something like

```auto
find_file(SetEnvFile
  NAME set-env.sh
  PATHS ${path-discovered-during-configuration}/*
  REQUIRED
)

add_custom_target(Foo
  COMMAND source ${SetEnvFile}
  COMMAND $(MAKE)
  WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)

```

However, I’ve found I actually can’t use `source` in my command. Do I have another option here? The trick in my situation is that CMake configuration (in my case CPM.cmake) figure out where the file is. So I can’t actually source it before running cmake.

The only workaround I can think of right now is to do something like

```auto
find_file(SetEnvFile
  NAME set-env.sh
  PATHS ${path-discovered-during-configuration}/*
  REQUIRED
)

add_custom_target(Foo
  COMMAND ./customBuildScript.sh ${SetEnvFile}
  WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
)

```

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [January 5, 2022, 2:22am UTC](https://discourse.cmake.org/t/custom-target-source-script/4775/2 "2022-01-05T02:22:19Z")

</div>

You’ll have to have a script source it internally before running what you want. That `customBuildScript.sh` pattern looks like what I’d end up doing.
