Skip to content

Commit

Permalink
Fix compatibility with Visual Studio 2017
Browse files Browse the repository at this point in the history
  • Loading branch information
crosire committed Nov 3, 2018
1 parent 58518e7 commit e712619
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 55 deletions.
5 changes: 3 additions & 2 deletions jetlink.vcxproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
Expand All @@ -21,13 +21,14 @@
<PropertyGroup Label="Globals">
<ProjectGuid>{188B7270-046B-476D-A32D-34390DA1A885}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<WindowsTargetPlatformVersion>10.0.17134.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Label="Configuration">
<OutDir>$(SolutionDir)\bin\$(Platform)\$(Configuration)\</OutDir>
<IntDir>$(SolutionDir)\obj\$(Platform)\$(Configuration)\</IntDir>
<ConfigurationType>Application</ConfigurationType>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v141</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
Expand Down
52 changes: 37 additions & 15 deletions source/jetlink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ namespace jetlink

for (auto it = paths.cbegin(); it != paths.cend(); ++it)
{
if (it->length() < length)
{
length = it->length();
continue;
}

const size_t l = std::mismatch(paths[0].cbegin(), paths[0].cend(), it->cbegin(), it->cend()).first - paths[0].cbegin();

if (l < length)
Expand All @@ -29,7 +35,7 @@ namespace jetlink
}
}

return paths[0].substr(0, paths[0].rfind('\\', length));
return paths[0].substr(0, length);
}

application::application() : _initialized(false), _executing(false), _compiler_stdin(INVALID_HANDLE_VALUE), _compiler_stdout(INVALID_HANDLE_VALUE)
Expand Down Expand Up @@ -97,8 +103,7 @@ namespace jetlink
_symbols.insert({ symbol.first, _imagebase + symbol.second });
}

_build_tool = pdb.buildtools()[0];
_sourcefiles = pdb.sourcefiles();
_source_files = pdb.sourcefiles();
}
else
{
Expand All @@ -114,16 +119,34 @@ namespace jetlink

_symbols.insert({ "__ImageBase", _imagebase });

for (const auto &path : _sourcefiles)
std::vector<std::string> cpp_files;

for (const auto &path : _source_files)
{
print(" Found source file: " + path + '\n');

// Let's add include directories for all source files and their parent folders
for (size_t i = 0, offset = std::string::npos; i < 2; ++i, --offset)
{
offset = path.find_last_of('\\', offset);
if (offset == std::string::npos)
break;
_include_dirs.insert(path.substr(0, offset));
}

if (path.rfind(".cpp") != std::string::npos && path.find("f:\\dd") == std::string::npos)
cpp_files.push_back(path);
}

_source_dir = longest_path(_sourcefiles);
_source_dir = longest_path(cpp_files);

print("Starting compiler process ...\n");
if (_source_dir.empty())
{
print(" Error: Could not find any source files.\n");
return;
}

print(" Using build tool: " + _build_tool + '\n');
print("Starting compiler process ...\n");

#pragma region // Launch compiler process
STARTUPINFO si = { sizeof(si) };
Expand Down Expand Up @@ -168,10 +191,12 @@ namespace jetlink

#pragma region // Set up compiler process environment variables
#if _M_IX86
const std::string command = "\"" + _build_tool + "\\..\\..\\vcvarsall.bat\" x86\n";
//const std::string command = "\"" + _build_tool + "\\..\\..\\vcvarsall.bat\" x86\n";
const std::string command = "\"C:\\Program Files (x86)\\Microsoft Visual Studio 15.0\\VC\\Auxiliary\\Build\\vcvarsall.bat\" x86\n";
#endif
#if _M_AMD64
const std::string command = "\"" + _build_tool + "\\..\\..\\..\\vcvarsall.bat\" x86_amd64\n";
//const std::string command = "\"" + _build_tool + "\\..\\..\\..\\vcvarsall.bat\" x86_amd64\n";
const std::string command = "\"C:\\Program Files (x86)\\Microsoft Visual Studio 15.0\\VC\\Auxiliary\\Build\\vcvarsall.bat\" x86_amd64\n";
#endif
DWORD size = 0;
WriteFile(_compiler_stdin, command.c_str(), static_cast<DWORD>(command.size()), &size, nullptr);
Expand Down Expand Up @@ -221,7 +246,7 @@ namespace jetlink

if (message.find("compile complete") != std::string::npos)
{
print("Finished compiling.\n");
print("Finished compiling \"" + _compiled_module_file + "\".\n");

_executing = false;
}
Expand Down Expand Up @@ -255,17 +280,14 @@ namespace jetlink
_executing = true;
_compiled_module_file = path.substr(0, path.find_last_of('.')) + ".obj";

std::vector<std::string> includes;
includes.push_back(_source_dir);

print("Detected modification to: " + path + '\n');

// Build compiler command line
std::string cmdline = "\"" + _build_tool + "\" /c /nologo /GS /W3 /Zc:wchar_t /Z7 /Od /fp:precise /errorReport:prompt /WX- /Zc:forScope /Gd /MDd /EHsc";
std::string cmdline = "cl.exe /c /nologo /GS /W3 /Zc:wchar_t /Z7 /Od /fp:precise /errorReport:prompt /WX- /Zc:forScope /Gd /MDd /EHsc /std:c++latest";
cmdline += " /Fo\"" + _compiled_module_file + "\"";
cmdline += " \"" + path + "\"";

for (const auto &include_path : includes)
for (const auto &include_path : _include_dirs)
{
cmdline += " /I \"" + include_path + "\"";
}
Expand Down
6 changes: 4 additions & 2 deletions source/jetlink.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <memory>
#include <string>
#include <vector>
#include <unordered_set>
#include <unordered_map>

class filewatcher;
Expand All @@ -26,10 +27,11 @@ namespace jetlink

private:
uint8_t *_imagebase = nullptr;
std::vector<std::string> _sourcefiles;
std::vector<std::string> _source_files;
std::unordered_set<std::string> _include_dirs;
std::unordered_map<std::string, void *> _symbols;
std::unique_ptr<filewatcher> _filewatcher;
std::string _build_tool, _source_dir, _compiled_module_file;
std::string _source_dir, _compiled_module_file;
bool _initialized, _executing;
void *_compiler_stdin, *_compiler_stdout;
};
Expand Down
74 changes: 38 additions & 36 deletions source/jetlink_reader_pdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,17 +195,13 @@ namespace jetlink
pdb_reader::pdb_reader(const std::string &path) : msf_reader(path)
{
if (!msf_reader::is_valid() || msf_reader::stream_count() <= 4)
{
return;
}

// Read PDB info stream
msf_stream_reader pdbstream(msf_reader::stream(1));

if (pdbstream.size() == 0)
{
return;
}

const auto pdbheader = pdbstream.read<pdb_header>();

Expand All @@ -229,9 +225,7 @@ namespace jetlink
for (unsigned int i = 0; i < hash_table_size; i++)
{
if ((bitset_present[i / 32] & (1 << (i % 32))) == 0)
{
continue;
}

const auto name_offset = pdbstream.read<uint32_t>();
const auto stream_index = pdbstream.read<uint32_t>();
Expand All @@ -244,24 +238,17 @@ namespace jetlink
_named_streams.insert({ name, stream_index });
}

_is_valid = true;
_is_valid = stream_count() > 4;
}

std::vector<type> pdb_reader::types()
{
if (!is_valid() || stream_count() <= 2)
{
return { };
}

// Read type info (TPI stream)
msf_stream_reader stream(msf_reader::stream(2));
const auto header = stream.read<pdb_tpi_header>();

if (header.header_size + header.content_size != stream.size())
{
return { };
}
return {};

// Skip any additional bytes that were appended to the header
stream.seek(header.header_size);
Expand Down Expand Up @@ -755,19 +742,12 @@ namespace jetlink
}
std::unordered_map<std::string, ptrdiff_t> pdb_reader::symbols()
{
if (!is_valid() || stream_count() <= 3)
{
return { };
}

// Read debug info (DBI stream)
msf_stream_reader stream(msf_reader::stream(3));
const auto dbiheader = stream.read<pdb_dbi_header>();

if (dbiheader.signature != 0xFFFFFFFF)
{
return { };
}
return {};

// Read section headers
stream.seek(sizeof(pdb_dbi_header) + dbiheader.module_info_size + dbiheader.section_contribution_size + dbiheader.section_map_size + dbiheader.file_info_size + dbiheader.ts_map_size + dbiheader.ec_info_size);
Expand Down Expand Up @@ -837,19 +817,12 @@ namespace jetlink
}
std::vector<std::string> pdb_reader::buildtools()
{
if (!is_valid() || stream_count() <= 4)
{
return { };
}

// Read build info
msf_stream_reader stream(msf_reader::stream(4));
const auto header = stream.read<pdb_tpi_header>();

if (header.header_size + header.content_size != stream.size())
{
return { };
}
return {};

std::unordered_set<std::string> buildtools;
std::unordered_map<unsigned int, std::string> string_table;
Expand Down Expand Up @@ -894,19 +867,47 @@ namespace jetlink
}
std::vector<std::string> pdb_reader::sourcefiles()
{
if (!is_valid() || stream_count() <= 4)
#if 1
// Read debug info (DBI stream)
msf_stream_reader stream(msf_reader::stream(3));
const auto dbiheader = stream.read<pdb_dbi_header>();

// https://llvm.org/docs/PDB/DbiStream.html#file-info-substream
stream.seek(sizeof(pdb_dbi_header) + dbiheader.module_info_size + dbiheader.section_contribution_size + dbiheader.section_map_size);
const uint16_t num_modules = stream.read<uint16_t>();
stream.skip(2 + num_modules * 2);

uint32_t num_source_files = 0;
for (uint16_t i = 0; i < num_modules; ++i)
num_source_files += stream.read<uint16_t>();

std::vector<uint32_t> file_name_offsets;
file_name_offsets.reserve(num_source_files);
for (uint32_t i = 0; i < num_source_files; ++i)
{
return { };
file_name_offsets.push_back(stream.read<uint32_t>());
}

auto offset = stream.tell();

std::vector<std::string> source_files;
source_files.reserve(num_source_files);
for (uint32_t i = 0; i < num_source_files; ++i)
{
stream.seek(offset + file_name_offsets[i]);

const std::string source_file = stream.read<std::string>();
source_files.push_back(source_file);
}

return source_files;
#else
// Read build info
msf_stream_reader stream(msf_reader::stream(4));
const auto header = stream.read<pdb_tpi_header>();
if (header.header_size + header.content_size != stream.size())
{
return { };
}
return {};
std::unordered_set<std::string> sourcefiles;
std::unordered_map<unsigned int, std::string> string_table;
Expand Down Expand Up @@ -956,6 +957,7 @@ namespace jetlink
}
return std::vector<std::string>(sourcefiles.begin(), sourcefiles.end());
#endif
}
std::unordered_map<unsigned int, std::string> pdb_reader::names()
{
Expand Down

0 comments on commit e712619

Please sign in to comment.