View source with formatted comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        jan@swi-prolog.org
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2021, SWI-Prolog Solutions b.v.
    7    All rights reserved.
    8
    9    Redistribution and use in source and binary forms, with or without
   10    modification, are permitted provided that the following conditions
   11    are met:
   12
   13    1. Redistributions of source code must retain the above copyright
   14       notice, this list of conditions and the following disclaimer.
   15
   16    2. Redistributions in binary form must reproduce the above copyright
   17       notice, this list of conditions and the following disclaimer in
   18       the documentation and/or other materials provided with the
   19       distribution.
   20
   21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32    POSSIBILITY OF SUCH DAMAGE.
   33*/
   34
   35:- module(build_cmake,
   36          []).   37:- use_module(tools).   38
   39/** <module> CMake plugin to deal with build steps
   40
   41Manage a CMake project. This prefers  the `ninja` generator if available
   42in ``$PATH``.
   43*/
   44
   45:- multifile
   46    cmake_option/2.                     % +Env, -Option
   47
   48:- multifile
   49    prolog:build_file/2,
   50    prolog:build_step/4.                % Step, Tool, SrcDir, BuildDir
   51
   52prolog:build_file('CMakeLists.txt', cmake).
   53
   54prolog:build_step(configure, cmake, State0, State) :-
   55    ensure_build_dir(build, State0, State),
   56    findall(Opt, cmake_option(State, Opt), Argv, [..]),
   57    run_process(path(cmake), Argv,
   58                [ directory(State.bin_dir),
   59                  env(State.env)
   60                ]).
   61prolog:build_step(build, cmake, State0, State) :-
   62    ensure_build_dir(build, State0, State),
   63    run_process(path(cmake), ['--build', '.'],
   64                [ directory(State.bin_dir),
   65                  env(State.env)
   66                ]).
   67prolog:build_step(test, cmake, State0, State) :-
   68    ensure_build_dir(build, State0, State),
   69    (   directory_file_path(State.bin_dir, 'CTestTestfile.cmake', TestFile),
   70        exists_file(TestFile)
   71    ->  test_jobs(Jobs),
   72        run_process(path(ctest), ['-j', Jobs, '--output-on-failure'],
   73                    [ directory(State.bin_dir),
   74                      env(State.env)
   75                    ])
   76    ;   true
   77    ).
   78prolog:build_step(install, cmake, State0, State) :-
   79    ensure_build_dir(build, State0, State),
   80    run_process(path(cmake), ['--install', '.'],
   81                [ directory(State.bin_dir),
   82                  env(State.env)
   83                ]).
   84prolog:build_step(clean, cmake, State0, State) :-
   85    ensure_build_dir(build, State0, State),
   86    run_cmake_target(State, clean).
   87prolog:build_step(distclean, cmake, State, State) :-
   88    directory_file_path(State.src_dir, build, BinDir),
   89    (   exists_directory(BinDir)
   90    ->  delete_directory_and_contents(BinDir)
   91    ;   true
   92    ).
   93
   94%!  cmake_option(+State, -Define) is nondet.
   95
   96cmake_option(_, CDEF) :-
   97    current_prolog_flag(executable, Exe),
   98    format(atom(CDEF), '-DSWIPL=~w', [Exe]).
   99cmake_option(_, CDEF) :-
  100    prolog_install_prefix(Prefix),
  101    format(atom(CDEF), '-DCMAKE_INSTALL_PREFIX=~w', [Prefix]).
  102cmake_option(State, CDEF) :-
  103    cmake_build_type(State, Type),
  104    format(atom(CDEF), '-DCMAKE_BUILD_TYPE=~w', [Type]).
  105cmake_option(State, Opt) :-
  106    has_program(path(ninja), _, State.env),
  107    member(Opt, ['-G', 'Ninja']).
  108
  109run_cmake_target(State, Target) :-
  110    cmake_generator_file(Generator, File),
  111    directory_file_path(State.bin_dir, File, AbsFile),
  112    exists_file(AbsFile),
  113    run_process(path(Generator), [Target],
  114                [ directory(State.bin_dir),
  115                  env(State.env)
  116                ]).
  117
  118cmake_generator_file(ninja, 'build.ninja').
  119cmake_generator_file(make,  'Makefile').
  120
  121cmake_build_type(State, Type) :-
  122    Type = State.get(build_type),
  123    !.
  124cmake_build_type(_, Type) :-
  125    current_prolog_flag(cmake_build_type, PlType),
  126    project_build_type(PlType, Type),
  127    !.
  128cmake_build_type(_, 'Release').
  129
  130project_build_type('PGO', 'Release').
  131project_build_type('DEB', 'Release').
  132project_build_type(Type, Type).
  133
  134
  135test_jobs(Jobs) :-
  136    current_prolog_flag(cpu_count, Cores),
  137    Jobs is max(1, max(min(4,Cores), Cores//2))