Skip to content

Commit 94c1b18

Browse files
committed
Update some cmake functions.
1 parent abf215e commit 94c1b18

3 files changed

Lines changed: 150 additions & 67 deletions

File tree

src/common/config.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ void Config::load(const yaml &root)
137137
projects.clear();
138138

139139
// load subdirs
140-
auto sd = root["add_subdirectories"];
140+
auto sd = root["add_directories"];
141141
if (sd.IsDefined())
142142
{
143143
for (auto &d : get_sequence<path>(sd))

src/inserts/functions.cmake

Lines changed: 120 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -135,42 +135,32 @@ endfunction(remove_src_unix)
135135
# FUNCTION remove_src_dir
136136
########################################
137137

138-
function(remove_src_dir var)
139-
file(GLOB_RECURSE rm ${SDIR}/${var})
140-
if (rm AND src)
138+
function(remove_src_dir var)
139+
file(GLOB_RECURSE rm ${SDIR}/${var})
140+
if (rm AND src)
141141
list(REMOVE_ITEM src ${rm})
142142
set(src ${src} PARENT_SCOPE)
143143
endif()
144144
endfunction(remove_src_dir)
145145

146146
########################################
147-
# FUNCTION project_group
147+
# FUNCTION remove_src_dir_regex
148148
########################################
149149

150-
function(project_group target name)
151-
set_target_properties(${target} PROPERTIES FOLDER ${name})
152-
endfunction(project_group)
150+
function(remove_src_dir_regex var)
151+
if (src)
152+
list(FILTER src EXCLUDE REGEX "${var}")
153+
set(src ${src} PARENT_SCOPE)
154+
endif()
155+
endfunction(remove_src_dir_regex)
153156

154157
########################################
155-
# FUNCTION file_write_once
158+
# FUNCTION project_group
156159
########################################
157160

158-
function(file_write_once f c)
159-
# multiple instances safe
160-
set(once ${f}.cppan.once)
161-
string(SHA1 h "${c}")
162-
if (NOT EXISTS ${once})
163-
file_write_safe(${f} "${c}")
164-
file_write_safe(${once} "${h}")
165-
return()
166-
endif()
167-
file(READ ${once} h2)
168-
if (NOT "${h}" STREQUAL "${h2}")
169-
file_write_safe(${f} "${c}")
170-
file_write_safe(${once} "${h}")
171-
return()
172-
endif()
173-
endfunction(file_write_once)
161+
function(project_group target name)
162+
set_target_properties(${target} PROPERTIES FOLDER ${name})
163+
endfunction(project_group)
174164

175165
########################################
176166
# FUNCTION file_write_safe
@@ -363,6 +353,10 @@ endfunction(get_configuration_with_generator_unhashed)
363353
########################################
364354

365355
function(get_configuration_exe_unhashed out)
356+
#get_configuration_unhashed(config)
357+
#set(${out} ${config} PARENT_SCOPE)
358+
#return()
359+
366360
prepare_config_part(system "${CMAKE_SYSTEM_NAME}")
367361
prepare_config_part(processor "${CMAKE_HOST_SYSTEM_PROCESSOR}")
368362
set(config ${system}${CPPAN_CONFIG_PART_DELIMETER}${processor})
@@ -665,6 +659,27 @@ function(check_result_variable ret)
665659
message(FATAL_ERROR "Last execute_process() with message '${ARGN}' failed with error: ${ret}")
666660
endfunction(check_result_variable)
667661

662+
########################################
663+
# FUNCTION file_write_once
664+
########################################
665+
666+
function(file_write_once f c)
667+
# multiple instances safe
668+
set(once ${f}.cppan.once)
669+
string(SHA1 h "${c}")
670+
if (NOT EXISTS ${once})
671+
file_write_safe(${f} "${c}")
672+
file_write_safe(${once} "${h}")
673+
return()
674+
endif()
675+
file(READ ${once} h2)
676+
if (NOT "${h}" STREQUAL "${h2}")
677+
file_write_safe(${f} "${c}")
678+
file_write_safe(${once} "${h}")
679+
return()
680+
endif()
681+
endfunction(file_write_once)
682+
668683
########################################
669684
# FUNCTION replace_in_file_once
670685
########################################
@@ -677,9 +692,15 @@ function(replace_in_file_once f from to)
677692
# cannot set this file to bdir because multiple configs use
678693
# different bdirs and will do multiple replacements
679694
set(h ${f}.${h})
680-
681-
if (EXISTS ${h})
682-
return()
695+
set(ch ${f}.cppan.hash)
696+
697+
if (EXISTS ${h} AND EXISTS ${ch})
698+
file(READ ${ch} h2)
699+
file(READ ${f} fc)
700+
string(SHA1 h3 "${fc}")
701+
if ("${h2}" STREQUAL "${h3}")
702+
return()
703+
endif()
683704
endif()
684705

685706
set(lock ${f}.lock)
@@ -693,16 +714,23 @@ function(replace_in_file_once f from to)
693714
endif()
694715

695716
# double check
696-
if (EXISTS ${h})
697-
return()
717+
if (EXISTS ${h} AND EXISTS ${ch})
718+
file(READ ${ch} h2)
719+
file(READ ${f} fc)
720+
string(SHA1 h3 "${fc}")
721+
if ("${h2}" STREQUAL "${h3}")
722+
return()
723+
endif()
698724
endif()
699725

700726
file(READ ${f} fc)
701727
string(REPLACE "${from}" "${to}" fc "${fc}")
728+
string(SHA1 h2 "${fc}")
702729
file(WRITE "${f}" "${fc}")
703730

704731
# create flag file
705732
file(WRITE ${h} "")
733+
file(WRITE ${ch} "${h2}")
706734
endfunction(replace_in_file_once)
707735

708736
########################################
@@ -719,11 +747,20 @@ endfunction(delete_in_file_once)
719747

720748
function(push_front_to_file_once f what)
721749
string(SHA1 h "${f}${what}")
722-
string(SUBSTRING "${h}" 0 5 h)
750+
string(SUBSTRING "${h}" 0 5 h)
751+
752+
# cannot set this file to bdir because multiple configs use
753+
# different bdirs and will do multiple replacements
723754
set(h ${f}.${h})
724-
725-
if (EXISTS ${h})
726-
return()
755+
set(ch ${f}.cppan.hash)
756+
757+
if (EXISTS ${h} AND EXISTS ${ch})
758+
file(READ ${ch} h2)
759+
file(READ ${f} fc)
760+
string(SHA1 h3 "${fc}")
761+
if ("${h2}" STREQUAL "${h3}")
762+
return()
763+
endif()
727764
endif()
728765

729766
set(lock ${f}.lock)
@@ -737,15 +774,22 @@ function(push_front_to_file_once f what)
737774
endif()
738775

739776
# double check
740-
if (EXISTS ${h})
741-
return()
777+
if (EXISTS ${h} AND EXISTS ${ch})
778+
file(READ ${ch} h2)
779+
file(READ ${f} fc)
780+
string(SHA1 h3 "${fc}")
781+
if ("${h2}" STREQUAL "${h3}")
782+
return()
783+
endif()
742784
endif()
743785

744786
file(READ ${f} fc)
787+
string(SHA1 h2 "${what}\n\n${fc}")
745788
file(WRITE "${f}" "${what}\n\n${fc}")
746789

747790
# create flag file
748791
file(WRITE ${h} "")
792+
file(WRITE ${ch} "${h2}")
749793
endfunction(push_front_to_file_once)
750794

751795
########################################
@@ -754,11 +798,20 @@ endfunction(push_front_to_file_once)
754798

755799
function(push_back_to_file_once f what)
756800
string(SHA1 h "${f}${what}")
757-
string(SUBSTRING "${h}" 0 5 h)
801+
string(SUBSTRING "${h}" 0 5 h)
802+
803+
# cannot set this file to bdir because multiple configs use
804+
# different bdirs and will do multiple replacements
758805
set(h ${f}.${h})
759-
760-
if (EXISTS ${h})
761-
return()
806+
set(ch ${f}.cppan.hash)
807+
808+
if (EXISTS ${h} AND EXISTS ${ch})
809+
file(READ ${ch} h2)
810+
file(READ ${f} fc)
811+
string(SHA1 h3 "${fc}")
812+
if ("${h2}" STREQUAL "${h3}")
813+
return()
814+
endif()
762815
endif()
763816

764817
set(lock ${f}.lock)
@@ -772,17 +825,35 @@ function(push_back_to_file_once f what)
772825
endif()
773826

774827
# double check
775-
if (EXISTS ${h})
776-
return()
828+
if (EXISTS ${h} AND EXISTS ${ch})
829+
file(READ ${ch} h2)
830+
file(READ ${f} fc)
831+
string(SHA1 h3 "${fc}")
832+
if ("${h2}" STREQUAL "${h3}")
833+
return()
834+
endif()
777835
endif()
778836

779837
file(READ ${f} fc)
838+
string(SHA1 h2 "${fc}\n\n${what}")
780839
file(WRITE "${f}" "${fc}\n\n${what}")
781840

782841
# create flag file
783842
file(WRITE ${h} "")
843+
file(WRITE ${ch} "${h2}")
784844
endfunction(push_back_to_file_once)
785845

846+
########################################
847+
# FUNCTION copy_file_once
848+
########################################
849+
850+
# this functions prevents changing variables in current scope
851+
function(copy_file_once from to)
852+
if (NOT EXISTS ${to})
853+
execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different ${from} ${to})
854+
endif()
855+
endfunction(copy_file_once)
856+
786857
########################################
787858
# FUNCTION cppan_include
788859
########################################
@@ -837,17 +908,6 @@ function(check_type_alignment TYPE LANG NAME)
837908
set(${NAME} ${${NAME}} CACHE STRING "Alignment of type: ${TYPE}" FORCE)
838909
endfunction(check_type_alignment)
839910

840-
########################################
841-
# FUNCTION copy_file_once
842-
########################################
843-
844-
# this functions prevents changing variables in current scope
845-
function(copy_file_once from to)
846-
if (NOT EXISTS ${to})
847-
execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different ${from} ${to})
848-
endif()
849-
endfunction(copy_file_once)
850-
851911
########################################
852912
# FUNCTION find_moc_targets
853913
########################################
@@ -1038,6 +1098,12 @@ endfunction()
10381098
########################################
10391099

10401100
function(cppan_qt5_wrap_cpp outfiles)
1101+
if (CPPAN_LOCAL_PROJECT)
1102+
qt5_wrap_cpp(x ${ARGN})
1103+
set(${outfiles} ${x} PARENT_SCOPE)
1104+
return()
1105+
endif()
1106+
10411107
# get include dirs
10421108
qt5_get_moc_flags(moc_flags)
10431109

@@ -1092,9 +1158,9 @@ endmacro()
10921158
# MACRO cppan_re2c
10931159
########################################
10941160

1095-
macro(cppan_re2c f)
1161+
macro(cppan_re2c f ext)
10961162
set(i ${SDIR}/${f})
1097-
set(o ${BDIR_PRIVATE}/${f}.cpp)
1163+
set(o ${BDIR_PRIVATE}/${f}.${ext})
10981164
add_custom_command(OUTPUT ${o}
10991165
COMMAND pvt.cppan.demo.re2c.re2c -o ${o} ${i}
11001166
DEPENDS ${i}

src/printers/cmake.cpp

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1772,19 +1772,30 @@ void CMakePrinter::print_src_config_file(const path &fn) const
17721772
{
17731773
if (!exclude_from_build.empty())
17741774
{
1775-
auto cpp_regex_2_cmake_regex = [](auto &s)
1775+
auto cpp_regex_2_cmake_regex = [](const auto &s)
17761776
{
1777-
boost::replace_all(s, ".*", "*");
1777+
return boost::replace_all_copy(s, ".*", "*");
1778+
};
1779+
auto cpp_regex_2_cmake_regex2 = [](const auto &s)
1780+
{
1781+
/*auto s2 = boost::replace_all_copy(s, "*", ".*");
1782+
boost::replace_all(s2, "**", "*");
1783+
boost::replace_all(s2, "..*", ".*");
1784+
return s2;*/
1785+
return s;
17781786
};
17791787

17801788
config_section_title(ctx, "exclude files");
17811789
for (auto &f : exclude_from_build)
17821790
{
17831791
// try to remove twice (double check) - as a file and as a dir
17841792
auto s = normalize_path(f);
1785-
cpp_regex_2_cmake_regex(s);
1793+
s = cpp_regex_2_cmake_regex(s);
1794+
auto s2 = normalize_path(f);
1795+
s2 = cpp_regex_2_cmake_regex2(s2);
17861796
ctx.addLine("remove_src (\"" + s + "\")");
17871797
ctx.addLine("remove_src_dir(\"" + s + "\")");
1798+
ctx.addLine("remove_src_dir_regex(\"" + s2 + "\")");
17881799
ctx.addLine();
17891800
}
17901801
ctx.emptyLines();
@@ -2034,15 +2045,21 @@ endif()
20342045

20352046
{
20362047
auto bdir = pkg.getDirObj() / cppan_build_dir / "${config_dir}";
2037-
auto p = normalize_path(get_binary_path(pkg, bdir.string()));
2038-
ctx.if_("EXISTS \"" + p + "\"");
2039-
ctx.increaseIndent("target_include_directories (${this}");
2040-
if (d.flags[pfHeaderOnly])
2041-
ctx.addLine("INTERFACE " + p);
2042-
else
2043-
ctx.addLine((d.flags[pfExecutable] ? "PRIVATE " : "PUBLIC ") + p);
2044-
ctx.decreaseIndent(")");
2045-
ctx.endif();
2048+
auto p = get_binary_path(pkg, bdir.string());
2049+
auto add_dir = [&ctx, this](const auto &p2) mutable
2050+
{
2051+
auto p = normalize_path(p2);
2052+
ctx.if_("EXISTS \"" + p + "\"");
2053+
ctx.increaseIndent("target_include_directories (${this}");
2054+
if (d.flags[pfHeaderOnly])
2055+
ctx.addLine("INTERFACE " + p);
2056+
else
2057+
ctx.addLine((d.flags[pfExecutable] ? "PRIVATE " : "PUBLIC ") + p);
2058+
ctx.decreaseIndent(")");
2059+
ctx.endif();
2060+
};
2061+
add_dir(bdir);
2062+
add_dir(p);
20462063
}
20472064

20482065
ctx.else_();

0 commit comments

Comments
 (0)