Skip to content

Commit f20278a

Browse files
committed
Build system: make C string converters read files
Instead of converting stdin, the converters now open and read the files they are to convert to C strings. This avoids invoking `cat` in build steps, which helps portability to non-Unix systems.
1 parent 6376250 commit f20278a

File tree

6 files changed

+41
-16
lines changed

6 files changed

+41
-16
lines changed

src/ansi-c/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ add_executable(converter library/converter.cpp)
66
file(GLOB ansi_c_library_sources "library/*.c")
77

88
add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/cprover_library.inc"
9-
COMMAND cat ${ansi_c_library_sources} | $<TARGET_FILE:converter> > "${CMAKE_CURRENT_BINARY_DIR}/cprover_library.inc"
9+
COMMAND $<TARGET_FILE:converter> ${ansi_c_library_sources} > "${CMAKE_CURRENT_BINARY_DIR}/cprover_library.inc"
1010
DEPENDS converter ${ansi_c_library_sources})
1111

1212
add_executable(file_converter file_converter.cpp)
1313

1414
function(make_inc name)
1515
add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${name}.inc"
16-
COMMAND file_converter < "${CMAKE_CURRENT_SOURCE_DIR}/${name}.h" > "${CMAKE_CURRENT_BINARY_DIR}/${name}.inc"
16+
COMMAND file_converter "${CMAKE_CURRENT_SOURCE_DIR}/${name}.h" > "${CMAKE_CURRENT_BINARY_DIR}/${name}.inc"
1717
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/${name}.h" file_converter
1818
)
1919
endfunction(make_inc)

src/ansi-c/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ library_check: library/*.c
110110
touch $@
111111

112112
cprover_library.inc: library/converter$(EXEEXT) library/*.c
113-
cat library/*.c | library/converter$(EXEEXT) > $@
113+
library/converter$(EXEEXT) library/*.c > $@
114114

115115
%.inc: %.h file_converter$(EXEEXT)
116-
./file_converter$(EXEEXT) < $< > $@
116+
./file_converter$(EXEEXT) $< > $@
117117

118118
ansi_c_internal_additions$(OBJEXT): $(BUILTIN_FILES)
119119

src/ansi-c/file_converter.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Author: Daniel Kroening, [email protected]
99
/// \file
1010
/// Convert file contents to C strings
1111

12+
#include <fstream>
1213
#include <iostream>
1314
#include <string>
1415

@@ -37,10 +38,23 @@ static void convert_line(const std::string &line)
3738
std::cout << "\\n\"\n";
3839
}
3940

40-
int main()
41+
int main(int argc, char *argv[])
4142
{
4243
std::string line;
4344

44-
while(getline(std::cin, line))
45-
convert_line(line);
45+
for(int i = 1; i < argc; ++i)
46+
{
47+
std::ifstream input_file(argv[i]);
48+
49+
if(!input_file)
50+
{
51+
std::cerr << "Failed to open " << argv[i] << '\n';
52+
return 1;
53+
}
54+
55+
while(getline(input_file, line))
56+
convert_line(line);
57+
}
58+
59+
return 0;
4660
}

src/ansi-c/library/converter.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Author: Daniel Kroening, [email protected]
66
77
\*******************************************************************/
88

9-
9+
#include <fstream>
1010
#include <iostream>
1111
#include <string>
1212

@@ -53,19 +53,28 @@ static void convert_line(const std::string &line, bool first)
5353
}
5454
}
5555

56-
int main()
56+
int main(int argc, char *argv[])
5757
{
5858
std::string line;
5959
bool first = true;
6060

6161
std::cout << "{\n";
6262

63-
if(getline(std::cin, line))
63+
for(int i = 1; i < argc; ++i)
6464
{
65-
convert_line(line, true);
66-
first = false;
67-
while(getline(std::cin, line))
68-
convert_line(line, false);
65+
std::ifstream input_file(argv[i]);
66+
67+
if(!input_file)
68+
{
69+
std::cerr << "Failed to open " << argv[i] << '\n';
70+
return 1;
71+
}
72+
73+
while(getline(input_file, line))
74+
{
75+
convert_line(line, first);
76+
first = false;
77+
}
6978
}
7079

7180
if(!first)
@@ -74,4 +83,6 @@ int main()
7483
std::cout <<
7584
"{ 0, 0 }\n"
7685
"}";
86+
87+
return 0;
7788
}

src/cpp/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
file(GLOB cpp_library_sources "library/*.c")
22

33
add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/cprover_library.inc"
4-
COMMAND cat ${cpp_library_sources} | $<TARGET_FILE:converter> > "${CMAKE_CURRENT_BINARY_DIR}/cprover_library.inc"
4+
COMMAND $<TARGET_FILE:converter> ${cpp_library_sources} > "${CMAKE_CURRENT_BINARY_DIR}/cprover_library.inc"
55
DEPENDS converter ${cpp_library_sources})
66

77
################################################################################

src/cpp/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ library_check: library/*.c
7373
touch $@
7474

7575
cprover_library.inc: ../ansi-c/library/converter$(EXEEXT) library/*.c
76-
cat library/*.c | ../ansi-c/library/converter$(EXEEXT) > $@
76+
../ansi-c/library/converter$(EXEEXT) library/*.c > $@
7777

7878
generated_files: cprover_library.inc
7979

0 commit comments

Comments
 (0)