Fpm: How to list directory content

Created on 22 Jul 2020  路  14Comments  路  Source: fortran-lang/fpm

In order to move beyond #137, we need a cross-platform way to read all the *.f90 files in a directory. What is the best way to do that in Fortran?

All 14 comments

For cross-platform you need this.

Otherwise, I think the next best kludge would be to call execute_command_line('ls > tmp.txt'), then parse tmp.txt, and equivalent on Windows.

We need a temporary solution so that we are not stuck on this, and can move on, and in the meantime we can work on the full general solution.

I agree. I think wrapping around the shell commands on each OS is the way to go.

It seems like it. How do you determine which OS you are on from Fortran?

Perhaps we can test the value of an environment variable (get_environment_variable) that we know will be different between systems? Perhaps HOME, and then detect based on whether the value has forward slashes (Linux and macOS) or backslashes (Windows)? Just an idea, I've never done this myself.

Good idea, the GET_ENVIRONMENT_VARIABLE intrinsic will do this. That will get us started, and we can make this more robust / general later.

I'd put it behind a function and just use preprocessor directives. Something like:

function is_windows()
    logical :: is_windows

#ifdef (WIN_32)
    is_windows = .true.
#elif
    is_windows = .false.
#endif

Obviously you'll need to look up the proper syntax and tests, but that way, even if you change the way it's done, it shouldn't require changing any other code anywhere.

@everythingfunctional ah that's right, I think that should work.

I didn't know fpm supported preprocessing. :+1: in that case.

fpm isn't doing any preprocessing, but our currently only supported compiler does a bit ;). Should be enough to solve at least the "Is this Windows or not?" question.

I just tried: on Windows "dir /b" gives the bare list of file names, just as "ls". That means that only the command is different - not the format of the output (with out "/b" you get a lot of rubbish ;)).

@everythingfunctional so the ifdef trick does not work with gfortran to determine the OS, I tried that in https://github.com/fortran-lang/fpm/pull/142, and it does not work on linux, nor anywhere else... Let's further discuss this OS determination in #144.

Thanks @arjenmarkus. I will try to do this once #142 is merged, as I need to determine the OS type first to know which command to run.

Until _stdlib_ can be included, perhaps linking an ISO C binding abstraction layer to either POSIX and Win32 routines at compile time may be sufficient.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

certik picture certik  路  10Comments

urbanjost picture urbanjost  路  8Comments

certik picture certik  路  3Comments

certik picture certik  路  10Comments

certik picture certik  路  3Comments