# Fpm usage for already-written code

**URL:** <https://fortran-lang.discourse.group/t/fpm-usage-for-already-written-code/5004>\
**Category:** fpm\
**Created:** [January 10, 2023, 2:30pm UTC](https://fortran-lang.discourse.group/t/fpm-usage-for-already-written-code/5004 "2023-01-10T14:30:10Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shahid](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/shahid/32/3672_2.png) [@Shahid](https://fortran-lang.discourse.group/u/Shahid)\
**Post date:** [January 10, 2023, 2:30pm UTC](https://fortran-lang.discourse.group/t/fpm-usage-for-already-written-code/5004/1 "2023-01-10T14:30:10Z")

</div>

I’d like to know how to run the already-written code with fpm.

The link  
[https://fpm.fortran-lang.org/en/tutorial/hello-fpm.html](https://fpm.fortran-lang.org/en/tutorial/hello-fpm.html)

only demonstrates how to start a new project with fpm

The second question is related to this output

 ![tree](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/2/2352a51dcb902f7607a5087ad606cada8cc10b4a.png)

What does the **tree** represent in the upper link? It does not look like a command here!

> I am using Windows OS.

---

<div class="post-metadata">

**Author:** ![FedericoPerini](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/federicoperini/32/1750_2.png) [@FedericoPerini](https://fortran-lang.discourse.group/u/FedericoPerini)\
**Post date:** [January 10, 2023, 2:36pm UTC](https://fortran-lang.discourse.group/t/fpm-usage-for-already-written-code/5004/2 "2023-01-10T14:36:14Z")

</div>

Are you using MSYS2? If so, you can install the tree utility by running

```auto
pacman -S tree

```

---

<div class="post-metadata">

**Author:** ![gnikit](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/gnikit/32/1213_2.png) [@gnikit](https://fortran-lang.discourse.group/u/gnikit)\
**Post date:** [January 10, 2023, 6:28pm UTC](https://fortran-lang.discourse.group/t/fpm-usage-for-already-written-code/5004/3 "2023-01-10T18:28:51Z")

</div>

For already existing projects you will need to define a fpm.toml with all the location of your source files. That might not necessarily play nice with your existing project structure.

You might want to have a look at one of my open source repos that takes an existing repo and repackages it for fpm

> **[GitHub - gnikit/metis-fpm: The METIS partitioning library with a Fortran and...](https://github.com/gnikit/metis-fpm)**
>
> The METIS partitioning library with a Fortran and C API packaged for fpm - GitHub - gnikit/metis-fpm: The METIS partitioning library with a Fortran and C API packaged for fpm

If you have your source files under `src` it’s probably going to be a lot easier to build with fpm. The thing that you will probably miss is defining a set of compile flags, but we are in the process of getting that working in a future release.

---

<div class="post-metadata">

**Author:** ![Shahid](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/shahid/32/3672_2.png) [@Shahid](https://fortran-lang.discourse.group/u/Shahid)\
**Post date:** [January 11, 2023, 9:32am UTC](https://fortran-lang.discourse.group/t/fpm-usage-for-already-written-code/5004/4 "2023-01-11T09:32:21Z")

</div>

Thank you for the link; I’ll look into it. However, it does not appear to be simple.

---

<div class="post-metadata">

**Author:** ![urbanjost](https://avatars.discourse-cdn.com/v4/letter/u/0ea827/32.png) [@urbanjost](https://fortran-lang.discourse.group/u/urbanjost)\
**Post date:** [January 11, 2023, 1:31pm UTC](https://fortran-lang.discourse.group/t/fpm-usage-for-already-written-code/5004/5 "2023-01-11T13:31:14Z")

</div>

In many cases I start with “fpm new DIRNAME” or “fpm new . --backfill” in an existing directory. Without seeing your original file structure I am not positive that is better than manually creating the subdirectories, etc. I have found it simplest in most cases to start that way. I am curious why you did not use “fpm new” as it might point towards ways to improve that subcommand.

---

<div class="post-metadata">

**Author:** ![Shahid](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/shahid/32/3672_2.png) [@Shahid](https://fortran-lang.discourse.group/u/Shahid)\
**Post date:** [January 13, 2023, 3:31pm UTC](https://fortran-lang.discourse.group/t/fpm-usage-for-already-written-code/5004/6 "2023-01-13T15:31:55Z")

</div>

![files](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/9/94a896e8a0a164a7554ece7f2060e313ceb97650.png)

**main.f90** is the program and the rest are all modules.

So how do I work with it?

---

<div class="post-metadata">

**Author:** ![urbanjost](https://avatars.discourse-cdn.com/v4/letter/u/0ea827/32.png) [@urbanjost](https://fortran-lang.discourse.group/u/urbanjost)\
**Post date:** [January 13, 2023, 11:20pm UTC](https://fortran-lang.discourse.group/t/fpm-usage-for-already-written-code/5004/7 "2023-01-13T23:20:10Z")

</div>

If there are no other restrictions, change to the directory, backfill it, move the files into the expected locations and run, like this:

```bash
cd $TO_THE_DIRECTORY
fpm new . --backfill
mv main.f90 app/
mv *mod.f90 src/
fpm run

```

Those are the ULS (Unix-Like Systems) commands; but the only thing that would vary would be the move commands; which have to be OS-specific.

---

<div class="post-metadata">

**Author:** ![Shahid](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/shahid/32/3672_2.png) [@Shahid](https://fortran-lang.discourse.group/u/Shahid)\
**Post date:** [January 14, 2023, 9:10am UTC](https://fortran-lang.discourse.group/t/fpm-usage-for-already-written-code/5004/8 "2023-01-14T09:10:37Z")

</div>

It is working fine now

 ![fpm](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/0/014b8a230b89c453636fe30931334abdc425916f.png)

but seems like **fpm** is a bit slower

 ![gfortran](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/f/f1512bb68b2a1a6342a783d62357ae2d075fa141.png)

---

<div class="post-metadata">

**Author:** ![ivanpribec](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ivanpribec/32/3290_2.png) [@ivanpribec](https://fortran-lang.discourse.group/u/ivanpribec)\
**Post date:** [January 14, 2023, 11:58am UTC](https://fortran-lang.discourse.group/t/fpm-usage-for-already-written-code/5004/9 "2023-01-14T11:58:28Z")

</div>

Try with

```plaintext
$ fpm run <myprogram> --profile release

```

The meaning is the following:

```plaintext
 --profile PROF selects the compilation profile for the build.
                   Currently available profiles are "release" for
                   high optimization and "debug" for full debug options.
                   If --flag is not specified the "debug" flags are the
                   default.

```

(For fpm developers, should the `--flag` in the last sentence of the description be `--profile`?)

---

<div class="post-metadata">

**Author:** ![urbanjost](https://avatars.discourse-cdn.com/v4/letter/u/0ea827/32.png) [@urbanjost](https://fortran-lang.discourse.group/u/urbanjost)\
**Post date:** [January 14, 2023, 2:24pm UTC](https://fortran-lang.discourse.group/t/fpm-usage-for-already-written-code/5004/10 "2023-01-14T14:24:25Z")

</div>

It’s correct but a little confusing. If --flag is specified no profile is applied unless specified. This allows you complete control of the compiler options. If --flag is not specified the default is --profile debug.

So if --flag is specified and no profile is specified only the options specified with --flag are applied.

If both are specified the options from the profile are combined with the options specified with --flag.

```bash
fpm run ### same as "fpm run --profile debug"
fpm run --flag '-Wall -O3' ## only the options specified are applied
fpm run --flag "-march=broadwell" --profile release # use options from release profile plus specified option.

```
