Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ find_package(clang)

if(NOT MSVC)
find_library(LIBXML2 NAMES xml2 libxml2)
if(${LIBXML2} STREQUAL "LIBXML2-NOTFOUND")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of the worst parts of the CMake language is that all *-NOTFOUND strings evaluate to false.

if(NOT LIBXML2)
message(FATAL_ERROR "Could not find libxml2")
else()
message("${LIBXML2} found")
endif()

find_library(ZLIB NAMES z zlib libz)
if(${ZLIB} STREQUAL "ZLIB-NOTFOUND")
if(NOT ZLIB)
message(FATAL_ERROR "Could not find zlib")
else()
message("${ZLIB} found")
Expand Down Expand Up @@ -400,7 +400,6 @@ set(ZIG_SOURCES
"${CMAKE_SOURCE_DIR}/src/ir.cpp"
"${CMAKE_SOURCE_DIR}/src/ir_print.cpp"
"${CMAKE_SOURCE_DIR}/src/link.cpp"
"${CMAKE_SOURCE_DIR}/src/main.cpp"
"${CMAKE_SOURCE_DIR}/src/os.cpp"
"${CMAKE_SOURCE_DIR}/src/parser.cpp"
"${CMAKE_SOURCE_DIR}/src/range_set.cpp"
Expand Down Expand Up @@ -727,7 +726,7 @@ if(MSVC)
elseif(MINGW)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall -Werror -Wno-error=format= -Wno-error=format -Wno-error=format-extra-args")
else()
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Werror -Wall")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall -Werror")
endif()

if(MSVC)
Expand Down Expand Up @@ -756,7 +755,7 @@ set_target_properties(zig_cpp PROPERTIES
COMPILE_FLAGS ${EXE_CFLAGS}
)

add_executable(zig ${ZIG_SOURCES})
add_executable(zig "${CMAKE_SOURCE_DIR}/src/main.cpp" ${ZIG_SOURCES})
set_target_properties(zig PROPERTIES
COMPILE_FLAGS ${EXE_CFLAGS}
LINK_FLAGS ${EXE_LDFLAGS}
Expand Down Expand Up @@ -792,3 +791,27 @@ foreach(file ${ZIG_STD_FILES})
get_filename_component(file_dir "${ZIG_STD_DEST}/${file}" DIRECTORY)
install(FILES "${CMAKE_SOURCE_DIR}/std/${file}" DESTINATION "${file_dir}")
endforeach()

include(CheckCXXSourceCompiles)
include(CTest)

set(fuzzer_flags "-fsanitize=undefined,fuzzer")
set(CMAKE_REQUIRED_FLAGS "${fuzzer_flags}")
check_cxx_source_compiles("
#include <stddef.h>
#include <stdint.h>
extern \"C\" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
return 0;
}"
have_fuzzer_support)
if(BUILD_TESTING AND have_fuzzer_support)
add_executable(fuzz_test src/fuzz_test.cpp ${ZIG_SOURCES} ${ZIG_CPP_SOURCES})
target_compile_options(fuzz_test PRIVATE "${fuzzer_flags}")
target_link_libraries(fuzz_test PRIVATE
"${fuzzer_flags}"
${SOFTFLOAT_LIBRARIES}
${CLANG_LIBRARIES}
${LLD_LIBRARIES}
${LLVM_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT})
endif()
33 changes: 33 additions & 0 deletions src/fuzz_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#define main zig_main
#include "main.cpp"
#undef main

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
char tmp_file_name[] = "/tmp/fuzzXXXXXX";
int fd = mkstemp(tmp_file_name);
if (fd < 0) {
perror("Cannot create temporary file");
return 0;
}
FILE* f = fdopen(fd, "w");
if (f == NULL) {
perror("Cannot open file handle");
return 0;
}
const size_t num_written = fwrite(data, 1, size, f);
fclose(f);
if (num_written != size) {
fprintf(stderr, "Cannot write to file\n");
return 0;
}

char arg0[] = "zig";
char arg1[] = "build-obj";
char* argv[] = { arg0, arg1, tmp_file_name, nullptr };
int argc = sizeof(argv) / sizeof(argv[0]) - 1; // Exclude nullptr
zig_main(argc, argv);

remove(tmp_file_name);
return 0;
}