34
35:- module(build_cmake,
36 []). 37:- use_module(tools).
45:- multifile
46 cmake_option/2. 47
48:- multifile
49 prolog:build_file/2,
50 prolog:build_step/4. 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 ).
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))
CMake plugin to deal with build steps
Manage a CMake project. This prefers the
ninja
generator if available in$PATH
. */