diff --git a/CMakeLists.txt b/CMakeLists.txt index 789a4e67..2fed3b9d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -55,9 +55,7 @@ include(TableGen) find_package(Git REQUIRED) -if (NOT WIN32) - add_subdirectory( linux_linker ) -endif() +add_subdirectory( linux_linker ) if (CMAKE_SIZEOF_VOID_P EQUAL 4) set(ADDR 32) @@ -127,7 +125,6 @@ set(TARGET_INCLUDE_FILES set(TARGET_SOURCE_FILES common_clang.cpp options.cpp - pch_mgr.cpp options_compile.cpp ) diff --git a/cl_headers/CMakeLists.txt b/cl_headers/CMakeLists.txt index 3dd2ea4c..f49cdef4 100644 --- a/cl_headers/CMakeLists.txt +++ b/cl_headers/CMakeLists.txt @@ -25,7 +25,6 @@ add_custom_target ( opencl-c.h ) - function(pack_to_obj SRC DST TAG) add_custom_command ( OUTPUT ${DST} @@ -35,14 +34,10 @@ function(pack_to_obj SRC DST TAG) ) endfunction(pack_to_obj) -if(WIN32) - list(APPEND CL_HEADERS_SRC OpenCL.rc) -else() - pack_to_obj(opencl-c.h opencl-c.h.cpp "PCM_OPENCL_C_H") - list(APPEND CL_HEADERS_SRC - opencl-c.h.cpp - ) -endif() +pack_to_obj(opencl-c.h opencl-c.h.cpp "PCM_OPENCL_C_H") +list(APPEND CL_HEADERS_SRC + opencl-c.h.cpp +) add_library(${CL_HEADERS_LIB} OBJECT ${CL_HEADERS_SRC} diff --git a/cl_headers/OpenCL.rc b/cl_headers/OpenCL.rc deleted file mode 100644 index 955c3c3a..00000000 --- a/cl_headers/OpenCL.rc +++ /dev/null @@ -1,14 +0,0 @@ -// Microsoft Visual C++ generated resource script. -// -#include "resource.h" - -1 TEXTINCLUDE -BEGIN - "resource.h\0" -END - -// -// Module with OpenCL C declarations with corresponding headers -// - -OPENCL_C_H PCM "opencl-c.h" diff --git a/cl_headers/resource.h b/cl_headers/resource.h deleted file mode 100644 index c03b11bc..00000000 --- a/cl_headers/resource.h +++ /dev/null @@ -1,24 +0,0 @@ -/*****************************************************************************\ - -Copyright (c) Intel Corporation (2009-2017). - - INTEL MAKES NO WARRANTY OF ANY KIND REGARDING THE CODE. THIS CODE IS - LICENSED ON AN "AS IS" BASIS AND INTEL WILL NOT PROVIDE ANY SUPPORT, - ASSISTANCE, INSTALLATION, TRAINING OR OTHER SERVICES. INTEL DOES NOT - PROVIDE ANY UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY - DISCLAIMS ANY WARRANTY OF MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR ANY - PARTICULAR PURPOSE, OR ANY OTHER WARRANTY. Intel disclaims all liability, - including liability for infringement of any proprietary rights, relating to - use of the code. No license, express or implied, by estoppel or otherwise, - to any intellectual property rights is granted herein. - - \file resource.h - -\*****************************************************************************/ - -#ifndef __RESOURCE__ -#define __RESOURCE__ - -#define OPENCL_C_H "OPENCL_C_H" - -#endif /* __RESOURCE__ */ diff --git a/common_clang.cpp b/common_clang.cpp index eff10640..e693b0f0 100644 --- a/common_clang.cpp +++ b/common_clang.cpp @@ -18,7 +18,6 @@ Copyright (c) Intel Corporation (2009-2017). #include "common_clang.h" #include "pch_mgr.h" -#include "cl_headers/resource.h" #include "binary_result.h" #include "options.h" @@ -111,26 +110,11 @@ void CommonClangInitialize() { } } -static bool GetHeaders(std::vector &Result) { - struct {const char *ID; const char *Name;} Headers[] = { - {OPENCL_C_H, "opencl-c.h"}, - }; - - Result.clear(); - Result.reserve(sizeof(Headers) / sizeof(*Headers)); - - ResourceManager &RM = ResourceManager::instance(); - - for (auto Header:Headers) { - Resource R = RM.get_resource(Header.Name, Header.ID, "PCM", true); - if (!R) { - assert(0 && "Resource not found"); - return false; - } - - Result.push_back(R); - } +extern const char PCM_OPENCL_C_H[]; +extern unsigned int PCM_OPENCL_C_H_size; +static bool GetHeaders(std::vector &Result) { + Result.push_back(Resource(PCM_OPENCL_C_H, PCM_OPENCL_C_H_size, "opencl-c.h")); return true; } diff --git a/common_clang.map b/common_clang.map index 7dea9161..1a5f9449 100644 --- a/common_clang.map +++ b/common_clang.map @@ -6,7 +6,6 @@ global: Compile; Link; GetKernelArgInfo; - PCM_OPENCL_C_H*; }; local: *; }; diff --git a/linux_linker/linux_resource_linker.cpp b/linux_linker/linux_resource_linker.cpp index c0195bc1..75a356ce 100644 --- a/linux_linker/linux_resource_linker.cpp +++ b/linux_linker/linux_resource_linker.cpp @@ -26,7 +26,7 @@ int main(int argc, char **argv) if (!argv[1] || !strcmp(argv[1], "-h") || !strcmp(argv[1], "--help") || !argv[2] || !argv[3]) { - printf("Usage: bif_linker [input file] [output file] [symbol base name]\n"); + printf("Usage: %s [input file] [output file] [symbol base name]\n", argv[0]); return 0; } @@ -44,8 +44,8 @@ int main(int argc, char **argv) return 1; } - fprintf(output, "// This file is auto generated by bo_linker, DO NOT EDIT\n\n"); - fprintf(output, "unsigned char __attribute__((visibility(\"default\"))) %s[] =\n{\n ", argv[3]); + fprintf(output, "// This file is auto generated by %s, DO NOT EDIT\n\n", argv[0]); + fprintf(output, "extern const char %s[] =\n{\n ", argv[3]); int count = 0; int is_eol; @@ -58,7 +58,7 @@ int main(int argc, char **argv) } fprintf(output, "0x00\n};\n\n"); - fprintf(output, "unsigned int __attribute__((visibility(\"default\"))) %s_size = %d;\n\n", argv[3], count); + fprintf(output, "unsigned int %s_size = %d;\n\n", argv[3], count); fclose(input); fclose(output); diff --git a/pch_mgr.cpp b/pch_mgr.cpp deleted file mode 100644 index 67d7e7a9..00000000 --- a/pch_mgr.cpp +++ /dev/null @@ -1,243 +0,0 @@ -/*****************************************************************************\ - -Copyright (c) Intel Corporation (2009-2017). - - INTEL MAKES NO WARRANTY OF ANY KIND REGARDING THE CODE. THIS CODE IS - LICENSED ON AN "AS IS" BASIS AND INTEL WILL NOT PROVIDE ANY SUPPORT, - ASSISTANCE, INSTALLATION, TRAINING OR OTHER SERVICES. INTEL DOES NOT - PROVIDE ANY UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY - DISCLAIMS ANY WARRANTY OF MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR ANY - PARTICULAR PURPOSE, OR ANY OTHER WARRANTY. Intel disclaims all liability, - including liability for infringement of any proprietary rights, relating to - use of the code. No license, express or implied, by estoppel or otherwise, - to any intellectual property rights is granted herein. - - \file pch_mgr.cpp - -\*****************************************************************************/ - -#include "pch_mgr.h" - -#include "llvm/Object/ELF.h" -#include "llvm/ADT/Twine.h" - -#include -#include -#include -#include -#ifdef _WIN32 -#include -#else -#include -#include - -struct auto_dlclose { - auto_dlclose(void *module) : m_pModule(module) {} - - ~auto_dlclose() { - } - - void *get() { return m_pModule; } - - bool operator!() { return !m_pModule; } - - operator bool() { return m_pModule != NULL; } - - void *release() { - void *pTemp = m_pModule; - m_pModule = NULL; - return pTemp; - } - -private: - auto_dlclose(const auto_dlclose &); - - void *m_pModule; -}; -#endif - -ResourceManager ResourceManager::g_instance; - -void dummy() {} - -// returns the pointer to the buffer loaded from the resource with the given id -Resource ResourceManager::get_resource(const char *name, const char *id, - const char *type, - bool requireNullTerminate) { - llvm::sys::ScopedLock mutexGuard(m_lock); - - auto res = m_buffers.find(id); - if (res == m_buffers.end()) { - // lazy load the resource if not found in the cache - if (!load_resource(id, type, requireNullTerminate)) { - return Resource(); - } - } - - res = m_buffers.find(id); - assert(res != m_buffers.end()); - - const char *data = res->second.first; - size_t size = res->second.second; - return Resource(data, size, name); -} - -const char *ResourceManager::get_file(const char *path, bool binary, - bool requireNullTerminate, - size_t &out_size) { - llvm::sys::ScopedLock mutexGuard(m_lock); - - std::string key(path); - - if (m_buffers.find(key) == m_buffers.end()) { - // lazy load the resource if not found in the cache - load_file(path, binary, requireNullTerminate); - } - - assert(m_buffers.find(key) != m_buffers.end()); - out_size = m_buffers[key].second; - return m_buffers[key].first; -} - -const char* ResourceManager::realloc_buffer(const char *id, - const char* buf, size_t size, - bool requireNullTerminate) { - std::vector &buffer = m_allocations[id]; - - size_t alloc_size = requireNullTerminate ? size + 1 : size; - buffer.resize(alloc_size); - buffer.assign(buf, buf + size); - // The data in the buffer will be eventually passed to llvm::MemoryBufferMem - // ctor via argument of StringRef type. The Length of this StringRef will be - // = the 'size' argument of this function. There is an assert in - // llvm::MemoryBuffer::init checking that element *past the end* of the memory - // range passed via the ctor is '\0'. So we add it here. - buffer.push_back('\0'); - - return &buffer[0]; -} - -#ifdef _WIN32 -bool ResourceManager::GetResourceWin32(const char *id, const char *pszType, - const char *&res, size_t &size) { - HMODULE hMod = NULL; - // Get the handle to the current module - GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | - GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, - (LPCSTR)dummy, &hMod); - if (!hMod) { - return false; - } - - std::string ResName = ("\"" + llvm::Twine(id) + "\"").str(); - // Locate the resource - HRSRC hRes = FindResourceA(hMod, ResName.c_str(), pszType); - if (!hRes) { - return false; - } - - // Load the resource - HGLOBAL hBytes = LoadResource(hMod, hRes); - if (!hBytes) { - return false; - } - - // Get the base address to the resource. This call doesn't really lock it - res = (const char*) LockResource(hBytes); - if (!res) { - return false; - } - - // Get the buffer size - size = SizeofResource(hMod, hRes); - if (!size) { - return false; - } - - return true; -} -#else // WIN32 - -/** - * GetResourceUnix loads resource from shared library `lib`. - * - * If `relocate` argument is `false`, returned resource is only valid if: - * 1) library `lib` is loaded into memory before use of this function - * 2) resource will be used only while library is still loaded - * - * If relocate is `true`, resource will be memcpy'ed into an internal buffer, - * i.e. no resource will be valid as long as the ResourceManager is alive. - */ -bool ResourceManager::GetResourceUnix(const char *id, const char *pszType, - const char *lib, bool relocate, - const char *&res, size_t &size) { - - void *handle = dlopen(lib, RTLD_NOW); - if (!handle) { - return false; - } - - auto_dlclose module(handle); - - std::string name = (pszType + llvm::Twine("_") + llvm::Twine(id)).str(); - std::string size_name = (name.c_str() + llvm::Twine("_size")).str(); - - const void *size_ptr = dlsym(module.get(), size_name.c_str()); - if (!size_ptr) { - return false; - } - - size = *(const uint32_t *)size_ptr; - res = (const char *)dlsym(module.get(), name.c_str()); - - if (!res) { - return false; - } - - return true; -} -#endif // WIN32 - -bool ResourceManager::load_resource(const char *id, const char *pszType, - bool requireNullTerminate) { - // this function is called under lock - assert(m_buffers.find(id) == m_buffers.end()); - - const char *res = nullptr; - size_t size = 0; -#ifdef WIN32 - bool ok = GetResourceWin32(id, pszType, res, size); -#else - bool ok = GetResourceUnix(id, pszType, LIBCOMMON_CLANG_NAME, - false, res, size); -#endif - - if (!ok) { - return false; - } - - if (requireNullTerminate && res[size] != '\0') { - // reallocate the buffer to ensure the null termination - res = realloc_buffer(id, res, size, requireNullTerminate); - } - - m_buffers[id] = std::pair(res, size); - return true; -} - -// cache the content of the file to the internal buffers -void ResourceManager::load_file(const char *path, bool binary, - bool requireNullTerminate) { - std::string key(path); - std::ifstream fs(path, binary ? std::ios::binary : std::ios::in); - std::vector &buffer = m_allocations[key]; - - buffer.assign(std::istreambuf_iterator(fs), - std::istreambuf_iterator()); - - if (requireNullTerminate && buffer.size() > 0 && buffer.back() != '\0') { - buffer.push_back('\0'); - } - m_buffers[key] = - std::pair(buffer.data(), buffer.size()); -} diff --git a/pch_mgr.h b/pch_mgr.h index 59733e20..6c181cfe 100644 --- a/pch_mgr.h +++ b/pch_mgr.h @@ -16,13 +16,7 @@ Copyright (c) Intel Corporation (2009-2017). \*****************************************************************************/ -#include "llvm/Support/Mutex.h" - -#include -#include -#include #include -#include struct Resource { @@ -36,56 +30,10 @@ struct Resource m_name = ""; } - Resource(const char* data, size_t size, const std::string& name): + Resource(const char* data, size_t size, const char* name): m_data(data), m_size(size), m_name(name) {} bool operator!() { return m_data == nullptr; } }; - - -// Singleton class for resource management -// Its main purpose is to cache the buffers loaded from the resources -// but it could be easely extended to support file based buffers as well -class ResourceManager { -public: - static ResourceManager &instance() { return g_instance; } - - Resource get_resource(const char *name, const char *id, - const char *type, bool requireNullTerminate); - - const char *get_file(const char *path, bool binary, bool requireNullTerminate, - size_t &out_size); - -private: - ResourceManager() {} - - bool load_resource(const char *id, const char *pszType, - bool requireNullTerminate); - - void load_file(const char *path, bool binary, bool requireNullTerminate); - - const char* realloc_buffer(const char *id, const char* buf, size_t size, - bool requireNullTerminate); - -#ifdef _WIN32 - bool GetResourceWin32(const char *id, const char *pszType, - const char *&res, size_t &size); - -#else - bool GetResourceUnix(const char *id, const char *pszType, - const char *lib, bool relocate, - const char *&res, size_t &size); - -#endif - -private: - static ResourceManager g_instance; - llvm::sys::Mutex m_lock; - // map that caches the pointers to the loaded buffers and their sizes - // those buffers could be either the pointer to the loaded - // resource or to the cached buffers (stored in the m_allocations var below) - std::map> m_buffers; - std::map> m_allocations; -};