The OS name is usually known at compile time, unless one compiles ie in
Cygwin. I don't know what happens in WSL.
There are some approaches, ie either the C preprocessor, or a script, or
a ... Java program can determine some values and place them in a file.
BTW, the C preprocessor of gfortran supports some predefined variables
but the OS name isn't one of them. One can use instead a C preprocessing
directive, as gcc defines "__APPLE__", "__linux", "__CYGWIN__" and so on
on the building environment. But gfortran doesn't support them. So this
normally means a C routine.
Below there is a Java class that creates a small Fortran program. Of
course this adds one more compiler dependency. One could do it by hand,
one file per supported system (maybe too much).
Hope this helps,
Ev. Drikos
------------------------------------------------------------------------
gcj --main=jos jos.java -o jos && ./jos > x.F90 && gfc x.F90 && ./a.out
OR
javac jos.java && java jos -cygwin > x.F90 && gfc x.F90 && ./a.out
------------------------------------------------------------------------
class jos {
public jos(){}
public static void main(String argv[]){
String os_name = System.getProperty("
os.name");
String os_user = System.getProperty("
user.name");
String os_path_sep = System.getProperty("file.separator");
String os_prefix = os_path_sep.equals("/") ? "-" : "/";
String os_home = System.getProperty("user.home");
boolean dynamic = false;
if ( argv != null && argv.length>0 && argv[0] != null) {
if ((argv[0].equalsIgnoreCase("-cygwin") ||
argv[0].equalsIgnoreCase("/cygwin"))) {
dynamic = true;
}}
//One doesn't normally need /var/root as the home path
if ( os_user.equals("root") ) {
}
if ( os_name != null) {
if ( os_name.indexOf("Wind")>=0 && dynamic) {
os_prefix = "";
os_path_sep = "";
os_home = "";
os_name = "";
}
else {
os_home = new String(" ='" + os_home + "'");
os_name = new String(" ='" + os_name + "'");
os_path_sep = new String(" ='" + os_path_sep + "'");
os_prefix = new String(" ='" + os_prefix + "'");
}
}
System.out.println(" character(len=20) :: os_name " + os_name );
System.out.println(" character(len=255) :: homedir " + os_home );
System.out.println(" character(len=1) :: path_sep" + os_path_sep );
System.out.println(" character(len=1) :: prefix " + os_prefix );
System.out.println(" ");
if (dynamic)
System.out.println(" homedir = cygwin_home(os_name,path_sep,prefix) ");
System.out.println(" ");
System.out.println(" print *, ' OS:', os_name ");
System.out.println(" print *, 'Home:', homedir ");
System.out.println(" print *, 'File:', __FILE__ ");
System.out.println(" print *, 'Base File:', __BASE_FILE__ ");
System.out.println(" print *, 'Date:', __DATE__ ");
System.out.println(" print *, 'Time:', __TIME__ ");
System.out.println(" print *, 'TimeStamp:', __TIMESTAMP__ ");
System.out.println(" print *, 'Line:', __LINE__ ");
System.out.println(" print *, 'INCLUDE_LEVEL:', __INCLUDE_LEVEL__ ");
System.out.println(" print *, 'Counter:', __COUNTER__ ");
System.out.println(" ");
if (dynamic) {
System.out.println(" contains ");
System.out.println(" ");
System.out.println(" function
cygwin_home(os_name,path_sep,prefix) result(homedir) ");
System.out.println(" ");
System.out.println(" character(len=20) :: os_name ");
System.out.println(" character(len=255) :: homedir ");
System.out.println(" character(len=1) :: path_sep ");
System.out.println(" character(len=1) :: prefix ");
System.out.println(" character(len=20) :: user ");
System.out.println(" ");
System.out.println(" CALL get_environment_variable('PWD',
path_sep) ");
System.out.println(" !print *, 'path=', path ");
System.out.println(" ");
System.out.println(" if (path_sep == '/') then ");
System.out.println(" os_name = 'CygWin' ");
System.out.println(" CALL get_environment_variable('HOME',
homedir) ");
System.out.println(" prefix = '-'");
System.out.println(" else ");
System.out.println(" os_name = 'Windows' ");
System.out.println(" CALL
get_environment_variable('USERPROFILE', homedir) ");
System.out.println(" path_sep = '\\'");
System.out.println(" prefix = '/'");
System.out.println(" end if ");
System.out.println(" ");
System.out.println(" end ");
System.out.println(" ");
}//if dynamic
System.out.println(" end ");
System.exit(0);
}
}//class jos