diff options
author | Kyungwoo Lee <kyulee@microsoft.com> | 2015-12-03 14:26:05 -0800 |
---|---|---|
committer | Kyungwoo Lee <kyulee@microsoft.com> | 2015-12-03 20:41:18 -0800 |
commit | 1d34f6293740f4f79f8b362c2d605fa2a527d538 (patch) | |
tree | a716d441f4449582b25bf2de9b264ba56fd8ee19 | |
parent | 0bdc646c5955ca0c735a3976d01969b246e848ef (diff) | |
download | coreclr-1d34f6293740f4f79f8b362c2d605fa2a527d538.tar.gz coreclr-1d34f6293740f4f79f8b362c2d605fa2a527d538.tar.bz2 coreclr-1d34f6293740f4f79f8b362c2d605fa2a527d538.zip |
Refactoring resource string
This pulls out resource string handling out of mscorrc into top level.
The scripts (awk) are now parameterized to generate a unique string table for the
given name.
Added resourcestring.cpp to create an API "LoadResourceString".
Added a few macros into resourcestring.h, which are used for definition/uses of
the string table.
-rw-r--r-- | src/CMakeLists.txt | 34 | ||||
-rw-r--r-- | src/dlls/mscorrc/CMakeLists.txt | 35 | ||||
-rw-r--r-- | src/dlls/mscorrc/full/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/dlls/mscorrc/resourcestring.h | 23 | ||||
-rw-r--r-- | src/dlls/mscorrc/small/CMakeLists.txt | 23 | ||||
-rw-r--r-- | src/nativeresources/.gitmirror | 1 | ||||
-rw-r--r-- | src/nativeresources/CMakeLists.txt | 9 | ||||
-rw-r--r-- | src/nativeresources/processrc.awk (renamed from src/dlls/mscorrc/processrc.awk) | 9 | ||||
-rw-r--r-- | src/nativeresources/rctocpp.awk (renamed from src/dlls/mscorrc/rctocpp.awk) | 12 | ||||
-rw-r--r-- | src/nativeresources/rctopo.awk (renamed from src/dlls/mscorrc/rctopo.awk) | 0 | ||||
-rw-r--r-- | src/nativeresources/resourcestring.cpp | 63 | ||||
-rw-r--r-- | src/nativeresources/resourcestring.h | 33 | ||||
-rw-r--r-- | src/utilcode/ccomprc.cpp | 57 | ||||
-rw-r--r-- | src/utilcode/crossgen/CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/utilcode/dac/CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/utilcode/dyncrt/CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/utilcode/staticnohost/CMakeLists.txt | 5 |
17 files changed, 179 insertions, 135 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 31c37e5a39..7949679f8e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -34,6 +34,40 @@ if(CLR_CMAKE_PLATFORM_UNIX) # This prevents inclusion of standard C++ compiler headers set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -nostdinc++") endif(NOT CLR_CMAKE_PLATFORM_DARWIN) + + set (NATIVE_RESOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/nativeresources) + include_directories(${NATIVE_RESOURCE_DIR}) + set (RC_TO_CPP ${NATIVE_RESOURCE_DIR}/rctocpp.awk) + set (PROCESS_RC ${NATIVE_RESOURCE_DIR}/processrc.awk) + set (RESOURCE_STRING_HEADER_DIR ${NATIVE_RESOURCE_DIR}) + + # Create a command to create a C++ source file containing an array of + # NativeStringResource structs which represent the information from a + # given Windows .rc file. The target C++ file path is returned in the + # variable specified by the TARGET_FILE parameter. + function(build_resources SOURCE TARGET_NAME TARGET_FILE) + + get_compile_definitions(PREPROCESS_DEFINITIONS) + get_include_directories(INCLUDE_DIRECTORIES) + + set(PREPROCESSED_SOURCE ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}.rc.i) + set(RESOURCE_ENTRY_ARRAY_CPP ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}.cpp) + + add_custom_command( + OUTPUT ${RESOURCE_ENTRY_ARRAY_CPP} + # Preprocess the windows .rc file + COMMAND ${CMAKE_CXX_COMPILER} -E -P ${PREPROCESS_DEFINITIONS} ${INCLUDE_DIRECTORIES} -o ${PREPROCESSED_SOURCE} -x c ${SOURCE} + # Convert the preprocessed .rc file to a C++ file which will be used to make a static lib. + COMMAND ${AWK} -v name=${TARGET_NAME} -f ${RC_TO_CPP} -f ${PROCESS_RC} ${PREPROCESSED_SOURCE} >${RESOURCE_ENTRY_ARRAY_CPP} + DEPENDS ${SOURCE} ${RC_TO_CPP} ${PROCESS_RC} + ) + + include_directories(${RESOURCE_STRING_HEADER_DIR}) + set(${TARGET_FILE} ${RESOURCE_ENTRY_ARRAY_CPP} PARENT_SCOPE) + + endfunction() + + add_subdirectory(nativeresources) endif(CLR_CMAKE_PLATFORM_UNIX) add_subdirectory(utilcode) diff --git a/src/dlls/mscorrc/CMakeLists.txt b/src/dlls/mscorrc/CMakeLists.txt index e139bc3a30..f3288c97bc 100644 --- a/src/dlls/mscorrc/CMakeLists.txt +++ b/src/dlls/mscorrc/CMakeLists.txt @@ -4,38 +4,6 @@ if(WIN32) # remove /ltcg from resource-only libraries string(REPLACE "/LTCG" "" CMAKE_SHARED_LINKER_FLAGS_RELEASE ${CMAKE_SHARED_LINKER_FLAGS_RELEASE}) string(REPLACE "/LTCG" "" CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO ${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO}) -else() - - set (RC_TO_CPP ${CMAKE_CURRENT_SOURCE_DIR}/rctocpp.awk) - set (PROCESS_RC ${CMAKE_CURRENT_SOURCE_DIR}/processrc.awk) - set (RESOURCE_STRING_HEADER_DIR ${CMAKE_CURRENT_SOURCE_DIR}) - - # Create a command to create a C++ source file containing an array of - # NativeStringResource structs which represent the information from a - # given Windows .rc file. The target C++ file path is returned in the - # variable specified by the TARGET_FILE parameter. - function(build_resources SOURCE TARGET_NAME TARGET_FILE) - - get_compile_definitions(PREPROCESS_DEFINITIONS) - get_include_directories(INCLUDE_DIRECTORIES) - - set(PREPROCESSED_SOURCE ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}.rc.i) - set(RESOURCE_ENTRY_ARRAY_CPP ${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}.cpp) - - add_custom_command( - OUTPUT ${RESOURCE_ENTRY_ARRAY_CPP} - # Preprocess the windows .rc file - COMMAND ${CMAKE_CXX_COMPILER} -E -P ${PREPROCESS_DEFINITIONS} ${INCLUDE_DIRECTORIES} -o ${PREPROCESSED_SOURCE} -x c ${SOURCE} - # Convert the preprocessed .rc file to a C++ file which will be used to make a static lib. - COMMAND ${AWK} -f ${RC_TO_CPP} -f ${PROCESS_RC} ${PREPROCESSED_SOURCE} >${RESOURCE_ENTRY_ARRAY_CPP} - DEPENDS ${SOURCE} ${RC_TO_CPP} ${PROCESS_RC} - ) - - include_directories(${RESOURCE_STRING_HEADER_DIR}) - set(${TARGET_FILE} ${RESOURCE_ENTRY_ARRAY_CPP} PARENT_SCOPE) - - endfunction() - endif(WIN32) add_subdirectory(full) @@ -43,4 +11,5 @@ add_subdirectory(full) # Only add the small version of the resources if the platform is Windows. if(WIN32) add_subdirectory(small) -endif(WIN32)
\ No newline at end of file +endif(WIN32) + diff --git a/src/dlls/mscorrc/full/CMakeLists.txt b/src/dlls/mscorrc/full/CMakeLists.txt index 1e9d2d149a..726ff0a47b 100644 --- a/src/dlls/mscorrc/full/CMakeLists.txt +++ b/src/dlls/mscorrc/full/CMakeLists.txt @@ -11,7 +11,6 @@ if(WIN32) install (FILES ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/mscorrc.debug.pdb DESTINATION PDB) else() - build_resources(${CMAKE_CURRENT_SOURCE_DIR}/../include.rc mscorrc_debug TARGET_CPP_FILE) add_library(mscorrc_debug STATIC diff --git a/src/dlls/mscorrc/resourcestring.h b/src/dlls/mscorrc/resourcestring.h deleted file mode 100644 index 475705d015..0000000000 --- a/src/dlls/mscorrc/resourcestring.h +++ /dev/null @@ -1,23 +0,0 @@ -// -// Copyright (c) Microsoft. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. -// - -#ifndef __RESOURCE_STRING_H_ -#define __RESOURCE_STRING_H_ - -// Struct to contain a resource ID and its corresponding -// English language string. -struct NativeStringResource -{ - unsigned int resourceId; - const char* resourceString; -}; - -// Sorted array of all native string resources -extern const NativeStringResource nativeStringResources[]; - -// Number of entries in nativeStringResources -extern const int NUMBER_OF_NATIVE_STRING_RESOURCES; - -#endif // __RESOURCE_STRING_H_ diff --git a/src/dlls/mscorrc/small/CMakeLists.txt b/src/dlls/mscorrc/small/CMakeLists.txt index 3bd7fcd713..a86ae1a830 100644 --- a/src/dlls/mscorrc/small/CMakeLists.txt +++ b/src/dlls/mscorrc/small/CMakeLists.txt @@ -1,22 +1,11 @@ add_definitions(-DFX_VER_INTERNALNAME_STR=mscorrc.dll) -if(WIN32) +add_library(mscorrc SHARED + ../mscorrc.small.rc +) - add_library(mscorrc SHARED - ../mscorrc.small.rc - ) +# add the install targets +install (TARGETS mscorrc DESTINATION .) +install (FILES ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/mscorrc.pdb DESTINATION PDB) - # add the install targets - install (TARGETS mscorrc DESTINATION .) - install (FILES ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/mscorrc.pdb DESTINATION PDB) - -else() - - build_resources(${CMAKE_CURRENT_SOURCE_DIR}/../mscorrc.small.rc mscorrc TARGET_CPP_FILE) - - add_library(mscorrc STATIC - ${TARGET_CPP_FILE} - ) - -endif(WIN32) diff --git a/src/nativeresources/.gitmirror b/src/nativeresources/.gitmirror new file mode 100644 index 0000000000..f507630f94 --- /dev/null +++ b/src/nativeresources/.gitmirror @@ -0,0 +1 @@ +Only contents of this folder, excluding subfolders, will be mirrored by the Git-TFS Mirror.
\ No newline at end of file diff --git a/src/nativeresources/CMakeLists.txt b/src/nativeresources/CMakeLists.txt new file mode 100644 index 0000000000..5bc326492a --- /dev/null +++ b/src/nativeresources/CMakeLists.txt @@ -0,0 +1,9 @@ +project(nativeresourcestring) + +add_compile_options(-fPIC) + +add_library(nativeresourcestring + STATIC + resourcestring.cpp +) + diff --git a/src/dlls/mscorrc/processrc.awk b/src/nativeresources/processrc.awk index 71279ef7d2..1632753956 100644 --- a/src/dlls/mscorrc/processrc.awk +++ b/src/nativeresources/processrc.awk @@ -8,7 +8,9 @@ BEGIN { inStringTable = 0; inBeginEnd = 0; - writeheader(); + arrayName = "nativeStringResourceArray_" name; + tableName = "nativeStringResourceTable_" name; + writeheader(arrayName, tableName); } { if ($1 == "STRINGTABLE" && $2 == "DISCARDABLE") @@ -52,14 +54,11 @@ BEGIN { gsub(/L"/, "\"", $0); # join strings "..." "..." into one gsub(/" +"/, "", $0); - # remove all terminating newlines from the string - the msgfmt fails on those - # since it expects them to be at the end of the msgid as well - while(gsub(/\\n"/, "\"", $0)) {} # write the resource entry to the target file writestringentry(var, $0); } } END { - writefooter(); + writefooter(arrayName, tableName); } diff --git a/src/dlls/mscorrc/rctocpp.awk b/src/nativeresources/rctocpp.awk index 84b4b909fc..e631021b35 100644 --- a/src/dlls/mscorrc/rctocpp.awk +++ b/src/nativeresources/rctocpp.awk @@ -57,12 +57,13 @@ function writestringentry(id, str) } # Write file header and begin the array we will populate with the resources. -function writeheader() +function writeheader(arrayName, tableName) { print "// This code was generated by rctocpp.awk and is not meant to be modified manually." print "#include <resourcestring.h>"; print ""; - print "const NativeStringResource nativeStringResources[] = {"; + print "extern const NativeStringResourceTable " tableName ";"; + print "const NativeStringResource " arrayName "[] = {"; } # Write file footer @@ -71,10 +72,13 @@ function writeheader() # now write all the entries we received so far when this is called. # After we have written all the entries, we close the array and add a # constant for the size of the array for convenience. -function writefooter() +function writefooter(arrayName, tableName) { writesortedentries(); print "};"; print ""; - print "const int NUMBER_OF_NATIVE_STRING_RESOURCES = " numEntries ";"; + + print "const NativeStringResourceTable " tableName " = {"; + print numEntries ","; + print arrayName "};"; } diff --git a/src/dlls/mscorrc/rctopo.awk b/src/nativeresources/rctopo.awk index 5229585ff5..5229585ff5 100644 --- a/src/dlls/mscorrc/rctopo.awk +++ b/src/nativeresources/rctopo.awk diff --git a/src/nativeresources/resourcestring.cpp b/src/nativeresources/resourcestring.cpp new file mode 100644 index 0000000000..75fb1ececd --- /dev/null +++ b/src/nativeresources/resourcestring.cpp @@ -0,0 +1,63 @@ +// +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// + + +#include <stdio.h> +#include <utilcode.h> +#include "resourcestring.h" + +static int CompareNativeStringResources(const void *a, const void *b) +{ + unsigned int resourceIdA = ((NativeStringResource*)a)->resourceId; + unsigned int resourceIdB = ((NativeStringResource*)b)->resourceId; + + if (resourceIdA < resourceIdB) + return -1; + + if (resourceIdA == resourceIdB) + return 0; + + return 1; +} + +int LoadNativeStringResource(const NativeStringResourceTable &nativeStringResourceTable, unsigned int iResourceID, WCHAR* szBuffer, int iMax, int *pcwchUsed) +{ + int len = 0; + if (szBuffer && iMax) + { + // Search the sorted set of resources for the ID we're interested in. + NativeStringResource searchEntry = {iResourceID, NULL}; + NativeStringResource *resourceEntry = (NativeStringResource*)bsearch( + &searchEntry, + nativeStringResourceTable.table, + nativeStringResourceTable.size, + sizeof(NativeStringResource), + CompareNativeStringResources); + + if (resourceEntry != NULL) + { + len = PAL_GetResourceString(NULL, resourceEntry->resourceString, szBuffer, iMax); + } + else + { + // The resource ID wasn't found in our array. Fall back on returning the ID as a string. + len = _snwprintf(szBuffer, iMax - 1, W("[Undefined resource string ID:0x%X]"), iResourceID); + if ((len < 0) || (len == (iMax - 1))) + { + // Add string terminator if the result of _snwprintf didn't fit the buffer. + szBuffer[iMax - 1] = W('\0'); + len = iMax - 1; + } + } + } + + if (pcwchUsed) + { + *pcwchUsed = len; + } + + return S_OK; +} + diff --git a/src/nativeresources/resourcestring.h b/src/nativeresources/resourcestring.h new file mode 100644 index 0000000000..1545b3c48f --- /dev/null +++ b/src/nativeresources/resourcestring.h @@ -0,0 +1,33 @@ +// +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// + +#ifndef __RESOURCE_STRING_H_ +#define __RESOURCE_STRING_H_ + +// Struct to contain a resource ID and its corresponding +// English language string. +struct NativeStringResource +{ + unsigned int resourceId; + const char* resourceString; +}; + +struct NativeStringResourceTable +{ + const int size; + const NativeStringResource *table; +}; + +int LoadNativeStringResource(const NativeStringResourceTable &nativeStringResourceTable, unsigned int iResourceID, char16_t* szBuffer, int iMax, int *pcwchUsed); + +#define CONCAT(a, b) a ## b + +#define NATIVE_STRING_RESOURCE_TABLE(name) CONCAT(nativeStringResourceTable_, name) + +#define DECLARE_NATIVE_STRING_RESOURCE_TABLE(name) \ + extern const NativeStringResourceTable NATIVE_STRING_RESOURCE_TABLE(name) + +#endif // __RESOURCE_STRING_H_ + diff --git a/src/utilcode/ccomprc.cpp b/src/utilcode/ccomprc.cpp index 9e1cd8c1d1..da7d5c07e4 100644 --- a/src/utilcode/ccomprc.cpp +++ b/src/utilcode/ccomprc.cpp @@ -10,7 +10,11 @@ #include "ndpversion.h" #include "../dlls/mscorrc/resource.h" -#include "../dlls/mscorrc/resourcestring.h" +#ifdef FEATURE_PAL +#include "resourcestring.h" +#define NATIVE_STRING_RESOURCE_NAME mscorrc_debug +DECLARE_NATIVE_STRING_RESOURCE_TABLE(NATIVE_STRING_RESOURCE_NAME); +#endif #include "sstring.h" #include "stringarraylist.h" @@ -697,21 +701,6 @@ HRESULT CCompRC::LoadString(ResourceCategory eCategory, UINT iResourceID, __out_ return LoadString(eCategory, langId, iResourceID, szBuffer, iMax, pcwchUsed); } -// Used for comparing NativeStringResource elements by ID. -int CompareNativeStringResources(const void *a, const void *b) -{ - unsigned int resourceIdA = ((NativeStringResource*)a)->resourceId; - unsigned int resourceIdB = ((NativeStringResource*)b)->resourceId; - - if (resourceIdA < resourceIdB) - return -1; - - if (resourceIdA == resourceIdB) - return 0; - - return 1; -} - HRESULT CCompRC::LoadString(ResourceCategory eCategory, LocaleID langId, UINT iResourceID, __out_ecount(iMax) LPWSTR szBuffer, int iMax, int *pcwchUsed) { CONTRACTL @@ -875,40 +864,8 @@ HRESULT CCompRC::LoadString(ResourceCategory eCategory, LocaleID langId, UINT iR return hr; #else // !FEATURE_PAL - int len = 0; - if (szBuffer && iMax) - { - // Search the sorted set of resources for the ID we're interested in. - NativeStringResource searchEntry = {iResourceID, NULL}; - NativeStringResource *resourceEntry = (NativeStringResource*)bsearch( - &searchEntry, - nativeStringResources, - NUMBER_OF_NATIVE_STRING_RESOURCES, - sizeof(NativeStringResource), - CompareNativeStringResources); - - if (resourceEntry != NULL) - { - len = PAL_GetResourceString(m_pResourceDomain, resourceEntry->resourceString, szBuffer, iMax); - } - else - { - // The resource ID wasn't found in our array. Fall back on returning the ID as a string. - len = _snwprintf(szBuffer, iMax - 1, W("[Undefined resource string ID:0x%X]"), iResourceID); - if ((len < 0) || (len == (iMax - 1))) - { - // Add string terminator if the result of _snwprintf didn't fit the buffer. - szBuffer[iMax - 1] = W('\0'); - len = iMax - 1; - } - } - } - - if (pcwchUsed) - { - *pcwchUsed = len; - } - + LoadNativeStringResource(NATIVE_STRING_RESOURCE_TABLE(NATIVE_STRING_RESOURCE_NAME), iResourceID, + szBuffer, iMax, pcwchUsed); return S_OK; #endif // !FEATURE_PAL } diff --git a/src/utilcode/crossgen/CMakeLists.txt b/src/utilcode/crossgen/CMakeLists.txt index 1f427e1e63..70981e3214 100644 --- a/src/utilcode/crossgen/CMakeLists.txt +++ b/src/utilcode/crossgen/CMakeLists.txt @@ -6,3 +6,6 @@ list(APPEND UTILCODE_SOURCES add_precompiled_header(stdafx.h ../stdafx.cpp UTILCODE_SOURCES) add_library(utilcode_crossgen STATIC ${UTILCODE_SOURCES}) +if(CLR_CMAKE_PLATFORM_UNIX) + target_link_libraries(utilcode_crossgen nativeresourcestring) +endif(CLR_CMAKE_PLATFORM_UNIX) diff --git a/src/utilcode/dac/CMakeLists.txt b/src/utilcode/dac/CMakeLists.txt index de6157b31e..8a957be1ab 100644 --- a/src/utilcode/dac/CMakeLists.txt +++ b/src/utilcode/dac/CMakeLists.txt @@ -6,8 +6,9 @@ list(APPEND UTILCODE_SOURCES ../hostimpl.cpp) if(CLR_CMAKE_PLATFORM_UNIX) add_library(utilcode_dac STATIC ${UTILCODE_SOURCES}) + target_link_libraries(utilcode_dac nativeresourcestring) add_dependencies(utilcode_dac coreclrpal) else() add_precompiled_header(stdafx.h ../stdafx.cpp UTILCODE_SOURCES) add_library(utilcode_dac STATIC ${UTILCODE_SOURCES}) -endif(CLR_CMAKE_PLATFORM_UNIX)
\ No newline at end of file +endif(CLR_CMAKE_PLATFORM_UNIX) diff --git a/src/utilcode/dyncrt/CMakeLists.txt b/src/utilcode/dyncrt/CMakeLists.txt index 59f29ab61b..c2164f327c 100644 --- a/src/utilcode/dyncrt/CMakeLists.txt +++ b/src/utilcode/dyncrt/CMakeLists.txt @@ -1,8 +1,9 @@ if(CLR_CMAKE_PLATFORM_UNIX) add_library(utilcode STATIC ${UTILCODE_SOURCES}) + target_link_libraries(utilcode nativeresourcestring) add_dependencies(utilcode coreclrpal) else() add_precompiled_header(stdafx.h ../stdafx.cpp UTILCODE_SOURCES) add_library(utilcode STATIC ${UTILCODE_SOURCES}) -endif(CLR_CMAKE_PLATFORM_UNIX)
\ No newline at end of file +endif(CLR_CMAKE_PLATFORM_UNIX) diff --git a/src/utilcode/staticnohost/CMakeLists.txt b/src/utilcode/staticnohost/CMakeLists.txt index 6b63b39f7b..c9912502c2 100644 --- a/src/utilcode/staticnohost/CMakeLists.txt +++ b/src/utilcode/staticnohost/CMakeLists.txt @@ -9,3 +9,8 @@ if(WIN32) endif(WIN32) add_library(utilcodestaticnohost STATIC ${UTILCODE_SOURCES}) + +if(CLR_CMAKE_PLATFORM_UNIX) + target_link_libraries(utilcodestaticnohost nativeresourcestring) +endif(CLR_CMAKE_PLATFORM_UNIX) + |