-
Notifications
You must be signed in to change notification settings - Fork 115
Allow fpm to change the working directory #483
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| module fpm_os | ||
| use, intrinsic :: iso_c_binding, only : c_char, c_int, c_null_char, c_ptr, c_associated | ||
| use fpm_error, only : error_t, fatal_error | ||
| implicit none | ||
| private | ||
| public :: change_directory, get_current_directory | ||
|
|
||
| #ifndef _WIN32 | ||
| character(len=*), parameter :: pwd_env = "PWD" | ||
| #else | ||
| character(len=*), parameter :: pwd_env = "CD" | ||
| #endif | ||
|
|
||
| interface | ||
| function chdir(path) result(stat) & | ||
| #ifndef _WIN32 | ||
| bind(C, name="chdir") | ||
| #else | ||
| bind(C, name="_chdir") | ||
| #endif | ||
| import :: c_char, c_int | ||
| character(kind=c_char, len=1), intent(in) :: path(*) | ||
| integer(c_int) :: stat | ||
| end function chdir | ||
|
|
||
| function getcwd(buf, bufsize) result(path) & | ||
| #ifndef _WIN32 | ||
| bind(C, name="getcwd") | ||
| #else | ||
| bind(C, name="_getcwd") | ||
| #endif | ||
| import :: c_char, c_int, c_ptr | ||
| character(kind=c_char, len=1), intent(in) :: buf(*) | ||
| integer(c_int), value, intent(in) :: bufsize | ||
| type(c_ptr) :: path | ||
| end function getcwd | ||
| end interface | ||
|
|
||
| contains | ||
|
|
||
| subroutine change_directory(path, error) | ||
| character(len=*), intent(in) :: path | ||
| type(error_t), allocatable, intent(out) :: error | ||
|
|
||
| character(kind=c_char, len=1), allocatable :: cpath(:) | ||
| integer :: stat | ||
|
|
||
| allocate(cpath(len(path)+1)) | ||
| call f_c_character(path, cpath, len(path)+1) | ||
|
|
||
| stat = chdir(cpath) | ||
|
|
||
| if (stat /= 0) then | ||
| call fatal_error(error, "Failed to change directory to '"//path//"'") | ||
| end if | ||
| end subroutine change_directory | ||
|
|
||
| subroutine get_current_directory(path, error) | ||
| character(len=:), allocatable, intent(out) :: path | ||
| type(error_t), allocatable, intent(out) :: error | ||
|
|
||
| character(kind=c_char, len=1), allocatable :: cpath(:) | ||
| integer(c_int), parameter :: buffersize = 1000_c_int | ||
| type(c_ptr) :: tmp | ||
|
|
||
| allocate(cpath(buffersize)) | ||
|
|
||
| tmp = getcwd(cpath, buffersize) | ||
| if (c_associated(tmp)) then | ||
| call c_f_character(cpath, path) | ||
| else | ||
| call fatal_error(error, "Failed to retrieve current directory") | ||
| end if | ||
|
|
||
| end subroutine get_current_directory | ||
|
|
||
| subroutine f_c_character(rhs, lhs, len) | ||
| character(kind=c_char), intent(out) :: lhs(*) | ||
| character(len=*), intent(in) :: rhs | ||
| integer, intent(in) :: len | ||
| integer :: length | ||
| length = min(len-1, len_trim(rhs)) | ||
|
|
||
| lhs(1:length) = transfer(rhs(1:length), lhs(1:length)) | ||
| lhs(length+1:length+1) = c_null_char | ||
|
|
||
| end subroutine f_c_character | ||
|
|
||
| subroutine c_f_character(rhs, lhs) | ||
| character(kind=c_char), intent(in) :: rhs(*) | ||
| character(len=:), allocatable, intent(out) :: lhs | ||
|
|
||
| integer :: ii | ||
|
|
||
| do ii = 1, huge(ii) - 1 | ||
| if (rhs(ii) == c_null_char) then | ||
| exit | ||
| end if | ||
| end do | ||
| allocate(character(len=ii-1) :: lhs) | ||
| lhs = transfer(rhs(1:ii-1), lhs) | ||
|
|
||
| end subroutine c_f_character | ||
|
|
||
| end module fpm_os | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's fine for this PR, but down the road we should move all these platform specific ifdefs into a
platform.f90orcompatibility.f90file, and then justusesuch a file here.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was tempted to do:
But GFortran didn't like those, unfortunately.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was going to try exactly that to see if it works! Then we could have the
chdir_symbolinplatform.f90. As it is now, we'll have to move the wholechdirdefinition intoplatform.f90, which is fine too.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used string concatenation in the past with
bind(C, name=namespace//"...")so in principle this seems to work. But here I'm hitting:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this is not possible because the interface is a separate scoping unit not automatically inheriting the module scope. Using
importwill also not work here because it would make the parameter available after it has been used. That's a tricky one, maybe worth a thread on discourse?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Posted here: https://fortran-lang.discourse.group/t/constant-expressions-for-bind-c-name/1319
Let's see if somebody more knowledgeable in the Fortran standard can help us out here.