From 479c86d3dab3435fcf3590a519b7b44800f6e461 Mon Sep 17 00:00:00 2001 From: vil02 <65706193+vil02@users.noreply.github.com> Date: Mon, 16 Sep 2024 21:29:22 +0200 Subject: [PATCH 1/6] chore: add `build` to `.gitignore` --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 974748c..6675f37 100644 --- a/.gitignore +++ b/.gitignore @@ -33,3 +33,4 @@ .idea/ +/build/ From 8c05c6bb47aa52e482b05bc34e4ed3c6429af538 Mon Sep 17 00:00:00 2001 From: vil02 <65706193+vil02@users.noreply.github.com> Date: Mon, 16 Sep 2024 21:31:15 +0200 Subject: [PATCH 2/6] style: fix syntax error in `example_usage_heap_sort.f90` --- examples/sorts/example_usage_heap_sort.f90 | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/sorts/example_usage_heap_sort.f90 b/examples/sorts/example_usage_heap_sort.f90 index a394dcc..8f3fd9a 100644 --- a/examples/sorts/example_usage_heap_sort.f90 +++ b/examples/sorts/example_usage_heap_sort.f90 @@ -11,7 +11,6 @@ program test_heap_sort ! Initialize the test array array = (/ 12, 11, 13, 5, 6, 7, 3, 9, -1, 2, -12, 1 /) - n = size(array) ! Print the original array print *, "Original array:" From 875a7dafb49876a1426b0e0b85a662570d67d8d0 Mon Sep 17 00:00:00 2001 From: vil02 <65706193+vil02@users.noreply.github.com> Date: Mon, 16 Sep 2024 21:32:35 +0200 Subject: [PATCH 3/6] chore: setup project using CMake --- CMakeLists.txt | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..ef62b3a --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,50 @@ +cmake_minimum_required(VERSION 3.16) +project(FortranProject LANGUAGES Fortran) + +add_compile_options(-Wall -Wextra -Wpedantic) + +function(add_fortran_sources DIR SOURCES) + file(GLOB_RECURSE NEW_SOURCES "${DIR}/*.f90") + list(APPEND ${SOURCES} ${NEW_SOURCES}) + set(${SOURCES} ${${SOURCES}} PARENT_SCOPE) +endfunction() + +set(MODULE_SOURCES) +add_fortran_sources(${CMAKE_SOURCE_DIR}/modules MODULE_SOURCES) + +add_library(modules STATIC ${MODULE_SOURCES}) + +function(create_unique_name FILE_NAME OUTPUT_NAME) + file(RELATIVE_PATH REL_PATH "${CMAKE_SOURCE_DIR}" "${FILE_NAME}") + get_filename_component(CUR_EXT "${REL_PATH}" LAST_EXT) + string(REPLACE "/" "_" UNIQUE_NAME "${REL_PATH}") + string(REPLACE "${CUR_EXT}" "" UNIQUE_NAME "${UNIQUE_NAME}") + set(${OUTPUT_NAME} ${UNIQUE_NAME} PARENT_SCOPE) +endfunction() + + +file(GLOB_RECURSE TEST_FILES "${CMAKE_SOURCE_DIR}/tests/*.f90") + +foreach(TEST_FILE ${TEST_FILES}) + create_unique_name(${TEST_FILE} TEST_NAME) + add_executable(${TEST_NAME} ${TEST_FILE}) + target_link_libraries(${TEST_NAME} modules) + add_test(NAME ${TEST_NAME} COMMAND ${TEST_NAME}) +endforeach() + +file(GLOB_RECURSE EXAMPLE_FILES "${CMAKE_SOURCE_DIR}/examples/*.f90") + +foreach(EXAMPLE_FILE ${EXAMPLE_FILES}) + create_unique_name(${EXAMPLE_FILE} EXAMPLE_NAME) + add_executable(${EXAMPLE_NAME} ${EXAMPLE_FILE}) + target_link_libraries(${EXAMPLE_NAME} modules) + list(APPEND EXAMPLE_NAME_LIST run_${EXAMPLE_NAME}) + add_custom_target(run_${EXAMPLE_NAME} + COMMAND ${EXAMPLE_NAME} + DEPENDS ${EXAMPLE_NAME} + COMMENT "Running example: ${EXAMPLE_NAME}") +endforeach() + +enable_testing() + +add_custom_target(run_all_examples DEPENDS ${EXAMPLE_NAME_LIST}) From 99ac035c3c2915569005e23a56769b6b50036a1d Mon Sep 17 00:00:00 2001 From: vil02 <65706193+vil02@users.noreply.github.com> Date: Mon, 16 Sep 2024 21:44:25 +0200 Subject: [PATCH 4/6] chore: add `ci.yml` --- .github/workflows/ci.yml | 45 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..064c5b9 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,45 @@ +--- +name: ci + +'on': + workflow_dispatch: + push: + branches: + - main + pull_request: + +env: + build_path: ${{github.workspace}}/build + +jobs: + build_and_test: + runs-on: ${{matrix.os}} + strategy: + fail-fast: false + matrix: + os: [ubuntu-22.04, ubuntu-24.04] + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Display versions + run: | + gfortran --version + cmake --version + + - name: Create Build Directory + run: cmake -E make_directory ${{env.build_path}} + + - name: Configure CMake + working-directory: ${{env.build_path}} + run: cmake ../ + + - name: Build + working-directory: ${{env.build_path}} + run: cmake --build . + + - name: Test + working-directory: ${{env.build_path}} + run: ctest +... From f017a0c34fab9fec70047ceab75d1d741045cb2c Mon Sep 17 00:00:00 2001 From: vil02 <65706193+vil02@users.noreply.github.com> Date: Thu, 19 Sep 2024 09:58:03 +0200 Subject: [PATCH 5/6] chore: make examples ci friendly --- examples/maths/euclid_gcd.f90 | 12 +++--------- examples/maths/fibonacci.f90 | 9 ++------- 2 files changed, 5 insertions(+), 16 deletions(-) diff --git a/examples/maths/euclid_gcd.f90 b/examples/maths/euclid_gcd.f90 index ef4bf64..ccd9f3e 100644 --- a/examples/maths/euclid_gcd.f90 +++ b/examples/maths/euclid_gcd.f90 @@ -4,17 +4,11 @@ program euclid_gcd_program use gcd_module implicit none integer :: a, b, val - character(len=1024) :: msg - integer :: istat - print *, "Enter the two numbers (+ve integers): " - read(*, *, iostat=istat, iomsg=msg) a, b - if (istat /= 0) then - write(*, fmt='(2A)') 'error: ', trim(msg) - stop 1 - end if + a = 56 + b = 98 val = gcd(a, b) - print *, 'The greatest common divisor is: ', val + print *, 'The greatest common divisor of ', a, ' and ', b, ' is: ', val end program euclid_gcd_program diff --git a/examples/maths/fibonacci.f90 b/examples/maths/fibonacci.f90 index c9c340d..ed7b3cc 100644 --- a/examples/maths/fibonacci.f90 +++ b/examples/maths/fibonacci.f90 @@ -6,14 +6,9 @@ program example_fibonacci implicit none integer :: n - print *, 'Enter a number: ' - read *, n - if (n <= 0) then - print *, 'Number must be a positive integer.' - stop 1 - end if + n = 7 - print *, 'The Fibonacci number for the specified position is:' + print *, 'The Fibonacci number for the position', n, ' is:' print *, 'Recursive solution: ', fib_rec(n) print *, 'Iterative solution: ', fib_itr(n) From d8c1640c474ccbb4cbb668978b8898fcc9d385a5 Mon Sep 17 00:00:00 2001 From: vil02 <65706193+vil02@users.noreply.github.com> Date: Thu, 19 Sep 2024 17:57:40 +0200 Subject: [PATCH 6/6] chore: run examples in ci --- .github/workflows/ci.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 064c5b9..9c0fbb9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -42,4 +42,8 @@ jobs: - name: Test working-directory: ${{env.build_path}} run: ctest + + - name: Run examples + working-directory: ${{env.build_path}} + run: make run_all_examples ...