Skip to content

Commit

Permalink
Add source-link and reduce binary size
Browse files Browse the repository at this point in the history
- Add source-link NuGet package to allow VS to retrieve source from github
- Remove absolute paths from .pdb
- Build with /bRepo
- Remove usage of std::format to reduce binary size -> 288 Kbyte to 65 Kbyte
  • Loading branch information
vbaderks committed Oct 15, 2024
1 parent 55933d3 commit 3185575
Show file tree
Hide file tree
Showing 14 changed files with 86 additions and 447 deletions.
33 changes: 30 additions & 3 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
<AccelerateBuildsInVisualStudio>true</AccelerateBuildsInVisualStudio><!-- Enable faster builds for SDK style projects in Visual Studio 2022 17.5 and newer. -->
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>

<_CertificateThumbprint>$(CertificateThumbprint)</_CertificateThumbprint>
<_CertificateThumbprint Condition="'$(_CertificateThumbprint)' != ''">$(CertificateThumbprint)</_CertificateThumbprint>
<_CertificateThumbprint Condition="'$(_CertificateThumbprint)' == ''">b834c6c1d7e0ae8e76cadcf9e2e7a273133a5df6</_CertificateThumbprint>
<_TimestampUrl>$(TimestampUrl)</_TimestampUrl>
<_TimestampUrl Condition="'$(_TimestampUrl)' != ''"> $(TimestampUrl)</_TimestampUrl>
<_TimestampUrl Condition="'$(_TimestampUrl)' == ''">http://time.certum.pl/</_TimestampUrl>
</PropertyGroup>

Expand Down Expand Up @@ -114,11 +114,15 @@
* WINRT *
WINRT_LEAN_AND_MEAN: Disables rarely-used features (in order to reduce compile times)
WINRT_NO_SOURCE_LOCATION (prevents full namespace names in the binary).
* Secure C Runtime *
__STDC_WANT_SECURE_LIB__=1;
* Microsoft STL *
_USE_DETAILED_FUNCTION_NAME_IN_SOURCE_LOCATION=0: Keep function name info short to reduce binary size.
-->
<PreprocessorDefinitions>WIN32_LEAN_AND_MEAN;NOSERVICE;NOMCX;NOIME;NOMINMAX;WINRT_LEAN_AND_MEAN;__STDC_WANT_SECURE_LIB__=1;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>WIN32_LEAN_AND_MEAN;NOSERVICE;NOMCX;NOIME;NOMINMAX;WINRT_LEAN_AND_MEAN;WINRT_NO_SOURCE_LOCATION;__STDC_WANT_SECURE_LIB__=1;_USE_DETAILED_FUNCTION_NAME_IN_SOURCE_LOCATION=0;%(PreprocessorDefinitions)</PreprocessorDefinitions>

<PrecompiledHeader>NotUsing</PrecompiledHeader>

Expand All @@ -133,6 +137,7 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<TreatLinkerWarningAsErrors>true</TreatLinkerWarningAsErrors>
<MinimumRequiredVersion>10</MinimumRequiredVersion>
<EnableUAC>false</EnableUAC>

<!-- Indicate DLL is compatible with Hardware supported Shadow stack (requires Zen3+ or Intel 11th Gen CPU) -->
<CETCompat Condition="'$(Platform)'!='ARM64'">true</CETCompat>
Expand All @@ -148,18 +153,40 @@
</Link>
</ItemDefinitionGroup>

<PropertyGroup Condition="'$(Configuration)'=='Debug'">
<LinkIncremental>true</LinkIncremental>
<UseDebugLibraries>true</UseDebugLibraries>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Release'">
<LinkIncremental>false</LinkIncremental>
<UseDebugLibraries>false</UseDebugLibraries>
<RunCodeAnalysis>true</RunCodeAnalysis>
<EnableCppCoreCheck>true</EnableCppCoreCheck>
<EnableMicrosoftCodeAnalysis>true</EnableMicrosoftCodeAnalysis>
<EnableClangTidyCodeAnalysis>false</EnableClangTidyCodeAnalysis>
<WholeProgramOptimization>true</WholeProgramOptimization>
</PropertyGroup>

<ItemDefinitionGroup Condition="'$(Configuration)'=='Release'">
<ClCompile>
<!-- For release builds, enable the MS static analyzer build into the C++ compiler. -->
<EnablePREfast>true</EnablePREfast>
<DisableAnalyzeExternal>true</DisableAnalyzeExternal>
<AdditionalOptions>%(AdditionalOptions) /Brepro</AdditionalOptions>
<RuntimeTypeInfo>false</RuntimeTypeInfo>
<UseFullPaths>false</UseFullPaths>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>

<!-- Store only the filename of the PDB file into the .exe/.dll for deterministic builds and use /Brepo. -->
<AdditionalOptions>/PDBALTPATH:%_PDB% /Brepro %(AdditionalOptions)</AdditionalOptions>

</Link>
</ItemDefinitionGroup>
</Project>
2 changes: 1 addition & 1 deletion src/class_factory.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct class_factory : winrt::implements<class_factory<Class>, IClassFactory>
}
catch (...)
{
return winrt::to_hresult();
return to_hresult();
}

HRESULT __stdcall LockServer(BOOL) noexcept override
Expand Down
22 changes: 10 additions & 12 deletions src/dll_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import util;
import property_store;

using std::array;
using std::format;
using std::uint32_t;
using std::wstring;
using namespace std::string_literals;
Expand Down Expand Up @@ -78,11 +77,11 @@ void register_decoder_file_extension(const wchar_t* file_type_name, const wchar_
constexpr wchar_t class_id_thumbnail_provider[]{L"{e357fccd-a995-4576-b01f-234630154e96}"};
constexpr wchar_t class_id_photo_thumbnail_provider[]{L"{c7657c4a-9f68-40fa-a4df-96bc08eb3551}"};

registry::set_value(format(LR"(SOFTWARE\Classes\{}\ShellEx\{})", file_type_name, class_id_thumbnail_provider), L"",
registry::set_value(LR"(SOFTWARE\Classes\)"s + file_type_name + LR"(\ShellEx\)" + class_id_thumbnail_provider, L"",
class_id_photo_thumbnail_provider);
registry::set_value(
format(LR"(SOFTWARE\Classes\SystemFileAssociations\{}\ShellEx\{})", file_extension, class_id_thumbnail_provider),
L"", class_id_photo_thumbnail_provider);
registry::set_value(LR"(SOFTWARE\Classes\SystemFileAssociations\)"s + file_extension + LR"()\ShellEx\)" +
class_id_thumbnail_provider,
L"", class_id_photo_thumbnail_provider);

// Register with the legacy Windows Photo Viewer (still installed on Windows 11 and 10): just forward to the TIFF
// registration.
Expand All @@ -93,7 +92,7 @@ void register_decoder_file_extension(const wchar_t* file_type_name, const wchar_
void register_decoder_pattern(const wstring& sub_key, const int index, const std::span<const std::byte> pattern)
{
const wstring patterns_sub_key{sub_key + LR"(\Patterns\)" + std::to_wstring(index)};
constexpr array mask{0xFF_byte, 0xFF_byte};
constexpr array mask{std::byte{0xFF}, std::byte{0xFF}};
registry::set_value(patterns_sub_key, L"Length", static_cast<uint32_t>(pattern.size()));
registry::set_value(patterns_sub_key, L"Position", 0U);
registry::set_value(patterns_sub_key, L"Mask", mask);
Expand Down Expand Up @@ -122,8 +121,7 @@ void register_property_store_file_extension(const wchar_t* file_extension)
L"", guid_to_string(id::property_store_class).c_str());

const auto sub_key = LR"(SOFTWARE\Classes\SystemFileAssociations\)"s + file_extension;
registry::set_value(sub_key, L"ExtendedTileInfo",
L"prop:System.ItemType;*System.DateModified;*System.Image.Dimensions");
registry::set_value(sub_key, L"ExtendedTileInfo", L"prop:System.ItemType;*System.DateModified;*System.Image.Dimensions");
registry::set_value(
sub_key, L"FullDetails",
L"prop:System.PropGroup.Image;System.Image.Dimensions;System.Image.HorizontalSize;System.Image.VerticalSize;System."
Expand Down Expand Up @@ -213,13 +211,13 @@ try
}
catch (...)
{
return winrt::to_hresult();
return to_hresult();
}

// Purpose: Used to determine whether the COM sub-system can unload the DLL from memory.
extern "C" __control_entrypoint(DllExport) HRESULT __stdcall DllCanUnloadNow()
{
const auto result{winrt::get_module_lock() ? S_FALSE : S_OK};
const auto result{winrt::get_module_lock() ? success_false : success_ok};
TRACE("netpbm-wic-codec::DllCanUnloadNow hr = {} (0 = S_OK -> unload OK)\n", result);
return result;
}
Expand All @@ -237,7 +235,7 @@ try
}
catch (...)
{
TRACE("netpbm-wic-codec::DllRegisterServer failed hr = {}\n", winrt::to_hresult());
TRACE("netpbm-wic-codec::DllRegisterServer failed hr = {}\n", to_hresult());
return self_registration::error_class;
}

Expand All @@ -250,7 +248,7 @@ try
}
catch (...)
{
const HRESULT hresult{winrt::to_hresult()};
const HRESULT hresult{to_hresult()};
TRACE("netpbm-wic-codec::DllUnregisterServer failed hr = {}\n", hresult);
return hresult;
}
Expand Down
1 change: 1 addition & 0 deletions src/hresults.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ constexpr HRESULT error_no_aggregation{CLASS_E_NOAGGREGATION};
constexpr HRESULT error_class_not_available{CLASS_E_CLASSNOTAVAILABLE};
constexpr HRESULT error_invalid_argument{E_INVALIDARG};
constexpr HRESULT error_access_denied{STG_E_ACCESSDENIED};
constexpr HRESULT error_out_of_memory{E_OUTOFMEMORY};

namespace self_registration {

Expand Down
Loading

0 comments on commit 3185575

Please sign in to comment.