NAG Fortran Compiler - VS Code Project Template
User Manual (for Mac)

※ This document was produced by Hosei University Honorary Professor Norio Takeuchi and Nihon Numerical Algorithms Group.

Download

To download this project template, please visit here.

After downloading the zip file, please unzip it to a location of your choice. The "NagFortranProjectTemplate" folder contained therein will be the project template itself.

Environment Setup

Before using this template, it is necessary to prepare the Fortran compiler, Visual Studio Code itself, Python, etc.

  1. NAG Fortran Compiler Installation
    First, if the NAG Fortran Compiler (Apple Arm Mac 64-bit version) is not yet installed, please install it.
  2. Visual Studio Code Installation
    Please download Visual Studio Code (macOS Universal) from the official website.
    VSCode download site
    Double-click the downloaded file and drag & drop the Visual Studio Code application into the Applications folder from the opened screen.
    Drag and drop VSCode
  3. Starting Visual Studio Code
    Double-click the Visual Studio Code application in the Applications folder to start Visual Studio Code.
    Clicking the VSCode icon
  4. Installation of the 'Modern Fortran' Extension
    Modern Fortran is an extension for VS Code that provides syntax highlighting for Fortran code, among other features. Please follow the steps below to install it within VS Code.
    (Note: The Modern Fortran extension is not mandatory for using this template)

    To install, first click the Extensions icon on the left side of the screen or press Shift + Command + X.
    VSCode extension button
    Next, type 'Fortran' into the search box at the top of the screen.
    Searching for Fortran
    Click 'Install' for the Modern Fortran extension that appears in the list.
    Modern Fortran extension
  5. Exiting Visutal Studio Code
    Once done, completely exit VS Code (not just closing the screen).
    VSCode exit menu
  6. Preparing the Python Environment
    This template uses Python 3.x for project management and build automation. Please ensure you can access Python 3.x from the command line. If Python 3.x is not yet installed, you will need to install it. Here, we introduce an example using Homebrew for installation.

    Open Terminal and start the installation by typing 'brew install python'.
    Example:
      brew install python
    
    Various messages will appear during the process, but towards the end, the folder where you can access Python will be displayed as follows.
    Installation information
    Next, add the displayed folder to the environment variable PATH in ~/.zshrc.
    Example setting:
      export PATH=/opt/homebrew/opt/python@3.11/libexec/bin:$PATH
    

With this, the environment setup is complete.

Creating a User Project

Next, we will explain how to create a user project.

  1. Copying and Renaming the Project Template
    Copy the project template (NagFortranProjectTemplate) to a location of your choice and rename it as desired.
    Please ensure that the folder path does not contain any spaces or Japanese characters. (Some systems may experience issues with these characters)
  2. Opening the Project Folder
    Launch VS Code and open the project folder by selecting File > Open Folder.
    Opening the project folder
  3. Setting the Project Name
    After opening the project folder, execute the Terminal > Run Task > Change Project Name task.
    Executing a task Changing the project name task
    An input window will appear on the screen for entering a new project name. Please enter the project name, ensuring not to include spaces or Japanese characters in the path.
    Entering the project name
  4. Placing the Source Code
    Create new or copy your user source code files (*.f90, *.f) into the `src` folder.
    Both `.f90` (free format) and `.f` (fixed format) extensions are acceptable.
  5. Here is an example of creating a new source code file.

    Create a new file under the source folder.
    Creating a new file
    Specify the file name.
    Setting the file name
    Enter your program.
    Entering the program

With this, your user project is ready.

Various Project Operations

For the completed project, the following operations are possible:

Below is an example of running the hello.f90 created earlier, using the Terminal > Run Task > Run Fortran Project menu.
Executing a task Running the task

You can confirm that the program has run successfully.
Execution screen

Changing Build Options

If you want to change the compile and link options, please modify the following parts in the Makefile as appropriate.

Red: Options for debug mode
Green: Options for release mode

# Compiler Options
FFLAGS_DEBUG := -g -gline -C
FFLAGS_RELEASE := -O2

# Linker Options
# examples
# LDFLAGS_DEBUG := -L/path/to/debug/libs -lmylibd
# LDFLAGS_RELEASE := -L/path/to/release/libs -lmylib
LDFLAGS_DEBUG :=
LDFLAGS_RELEASE :=

Makefile

The provided Makefile is designed to work with Unix-like OSes (Unix, Linux, macOS) as well as Windows. The main targets defined in this file are as follows:

dep-all
The default target when running the make command without arguments. It generates the dependency file (build_release/dependencies.mk or build_debug/dependencies.mk) if it does not exist, and then executes the all target. The dependency file is generated using the NAG Fortran Compiler's dependency analysis tool (nagfor =depend), which considers dependencies of Fortran modules.
all
Compiles all necessary source files to create the executable file.
run
Executes the program. The all target must be run first.
clean
Removes all files generated during the build process, including the dependencies.mk file.
show_info
Displays the current settings of the project (such as project name, build mode, etc.). The settings are read from "buildMode" in prjsettings.json.
set_debug_mode
Switches the build mode to debug. It updates "buildMode" in prjsettings.json using the write_settings.py script.
set_release_mode
Switches the build mode to release. It updates "buildMode" in prjsettings.json using the write_settings.py script.
change_project_name
Changes the project name. The change_project_name.py script is used to update "projectName" in prjsettings.json and also to set the path of the executable file name in .vscode/launcher.json, which changes according to the project name (for example, /home/nag/.../build_debug/myProjectDebug.exe).
depend
Generates the dependency file. It uses nagfor =depend to obtain dependencies. The information obtained is then transformed considering the build directory using transform_dependencies.py and finally output to dependencies.mk.

About the scripts/*.py Files

This template utilizes the following Python scripts:

  1. read_settings.py
  2. write_settings.py
  3. change_project_name.py
  4. transform_dependencies.py

About .prjsettings.json

This file is located in the root folder of the project and is used to manage the project name and the current build mode. It is in JSON format.

Example of .prjsettings.json:
{
    "buildMode": "debug",
    "projectName": "MyFortranProject"
}

Usage: