-
Notifications
You must be signed in to change notification settings - Fork 392
How to generate C++ source code via command line? #5890
-
For historical reasons, we have a CI pipeline that downloads the SDK nuget and generates the C++ headers. It uses cppwinrt.exe and some simple scripting. With the split of the nuget to several components, it's no longer so simple to generate it. Even if I resolve the dependencies from the nuspec file manually, I need to parse the targets, for example in Microsoft.WindowsAppSDK.AI:
<Reference Include="$(MSBuildThisFileDirectory)..\..\metadata\Microsoft.Windows.AI.winmd">
<Private>false</Private>
</Reference>
to know which .winmd files are to be passed to cppwinrt.exe.
Visual Studio does all this automatically when building a project.
Is there a way to generate the headers without having to do all the target parsing etc. manually and without Visual Studio (or msbuild and an ugly throwaway project)?
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment 4 replies
-
I didn't actually change anything with my 1.7 scripts. The big thing that I did was to write a script to basically replicate the 1.7 layout.
Screenshot 2025年10月05日 182211The biggest special case was the interactive experiences metapackage.
Screenshot 2025年10月05日 182139The contents of these should end up in the uap10.0.17763 and uap10.0.18362 directories. Otherwise the content should end up in uap. The metadata is in the metadata directory in the root of the package.
So the script essentailly had to extract the NuGet package, look for metadata, if it doesn't exist then ignore the package. If metadata exists, look for the10.0.17763.0 directory and the 10.0.18362.0 directory. If they exist, copy the contents of these directories the the above mentioned version directories. Otherwise copy the contents to the above named directory.
Beta Was this translation helpful? Give feedback.
All reactions
-
Thank you for replying, I appreciate it! I was afraid that hardcoding things such as the "metadata" name and relying on subfolders etc. isn't going to be reliable in the long term. Also I wasn't sure about "lib" folders and "-input" vs. "-ref" for cppwinrt.exe. Is your script published somewhere, by any chance?
Beta Was this translation helpful? Give feedback.
All reactions
-
Unfortunately, I can't publish it.
But to answer the question of -input vs. -ref, basically -input tells CppWinRT to generate headers for that metadata file, and -ref tells CppWinRT that the files exist but they won't be used for header generation. Basically, -ref is there for dependency resolution.
To give example information from one of the projects, what my script does is generates response files and then uses those files to execute CppWinRT. CppWinRT is executed twice, the first generates the headers for Windows:
-in "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.AI.MachineLearning.MachineLearningContract5円.0.0.0\Windows.AI.MachineLearning.MachineLearningContract.winmd"
-in "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.AI.MachineLearning.Preview.MachineLearningPreviewContract2円.0.0.0\Windows.AI.MachineLearning.Preview.MachineLearningPreviewContract.winmd"
-in "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.ApplicationModel.Calls.Background.CallsBackgroundContract4円.0.0.0\Windows.ApplicationModel.Calls.Background.CallsBackgroundContract.winmd"
-in "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.ApplicationModel.Calls.CallsPhoneContract7円.0.0.0\Windows.ApplicationModel.Calls.CallsPhoneContract.winmd"
-in "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.ApplicationModel.Calls.CallsVoipContract5円.0.0.0\Windows.ApplicationModel.Calls.CallsVoipContract.winmd"
-in "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.ApplicationModel.CommunicationBlocking.CommunicationBlockingContract2円.0.0.0\Windows.ApplicationModel.CommunicationBlocking.CommunicationBlockingContract.winmd"
-in "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.ApplicationModel.SocialInfo.SocialInfoContract2円.0.0.0\Windows.ApplicationModel.SocialInfo.SocialInfoContract.winmd"
-in "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.ApplicationModel.StartupTaskContract3円.0.0.0\Windows.ApplicationModel.StartupTaskContract.winmd"
-in "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.Devices.Custom.CustomDeviceContract1円.0.0.0\Windows.Devices.Custom.CustomDeviceContract.winmd"
-in "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.Devices.DevicesLowLevelContract3円.0.0.0\Windows.Devices.DevicesLowLevelContract.winmd"
-in "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.Devices.Printers.PrintersContract1円.0.0.0\Windows.Devices.Printers.PrintersContract.winmd"
-in "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.Devices.SmartCards.SmartCardBackgroundTriggerContract3円.0.0.0\Windows.Devices.SmartCards.SmartCardBackgroundTriggerContract.winmd"
-in "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.Devices.SmartCards.SmartCardEmulatorContract6円.0.0.0\Windows.Devices.SmartCards.SmartCardEmulatorContract.winmd"
-in "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.Foundation.FoundationContract4円.0.0.0\Windows.Foundation.FoundationContract.winmd"
-in "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.Foundation.UniversalApiContract19円.0.0.0\Windows.Foundation.UniversalApiContract.winmd"
-in "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.Gaming.XboxLive.StorageApiContract1円.0.0.0\Windows.Gaming.XboxLive.StorageApiContract.winmd"
-in "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.Graphics.Printing3D.Printing3DContract4円.0.0.0\Windows.Graphics.Printing3D.Printing3DContract.winmd"
-in "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.Networking.Connectivity.WwanContract3円.0.0.0\Windows.Networking.Connectivity.WwanContract.winmd"
-in "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.Networking.Sockets.ControlChannelTriggerContract3円.0.0.0\Windows.Networking.Sockets.ControlChannelTriggerContract.winmd"
-in "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.Security.Isolation.IsolatedWindowsEnvironmentContract5円.0.0.0\Windows.Security.Isolation.Isolatedwindowsenvironmentcontract.winmd"
-in "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.Services.Maps.GuidanceContract3円.0.0.0\Windows.Services.Maps.GuidanceContract.winmd"
-in "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.Services.Maps.LocalSearchContract4円.0.0.0\Windows.Services.Maps.LocalSearchContract.winmd"
-in "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.Services.Store.StoreContract4円.0.0.0\Windows.Services.Store.StoreContract.winmd"
-in "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.Services.TargetedContent.TargetedContentContract1円.0.0.0\Windows.Services.TargetedContent.TargetedContentContract.winmd"
-in "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.Storage.Provider.CloudFilesContract7円.0.0.0\Windows.Storage.Provider.CloudFilesContract.winmd"
-in "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.System.Profile.ProfileHardwareTokenContract1円.0.0.0\Windows.System.Profile.ProfileHardwareTokenContract.winmd"
-in "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.System.Profile.ProfileRetailInfoContract1円.0.0.0\Windows.System.Profile.ProfileRetailInfoContract.winmd"
-in "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.System.Profile.ProfileSharedModeContract2円.0.0.0\Windows.System.Profile.ProfileSharedModeContract.winmd"
-in "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.System.Profile.SystemManufacturers.SystemManufacturersContract3円.0.0.0\Windows.System.Profile.SystemManufacturers.SystemManufacturersContract.winmd"
-in "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.System.SystemManagementContract7円.0.0.0\Windows.System.SystemManagementContract.winmd"
-in "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.UI.UIAutomation.UIAutomationContract2円.0.0.0\Windows.UI.UIAutomation.UIAutomationContract.winmd"
-in "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.UI.ViewManagement.ViewManagementViewScalingContract1円.0.0.0\Windows.UI.ViewManagement.ViewManagementViewScalingContract.winmd"
-in "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.UI.Xaml.Core.Direct.XamlDirectContract5円.0.0.0\Windows.UI.Xaml.Core.Direct.XamlDirectContract.winmd"
Notice that all of these are specified using -in, CppWinRT accepts this as an abbreviation for -input.
Next, I run CppWinRT with the references to the Windows App SDK.
-in "G:\cmdline\packages\was\lib\uap10.0.18362\Microsoft.Foundation.winmd"
-in "G:\cmdline\packages\was\lib\uap10.0.18362\Microsoft.Graphics.winmd"
-in "G:\cmdline\packages\was\lib\uap10.0\Microsoft.UI.Text.winmd"
-in "G:\cmdline\packages\was\lib\uap10.0.18362\Microsoft.UI.winmd"
-in "G:\cmdline\packages\was\lib\uap10.0\Microsoft.Graphics.Imaging.winmd"
-in "G:\cmdline\packages\was\lib\uap10.0\Microsoft.Security.Authentication.OAuth.winmd"
-in "G:\cmdline\packages\was\lib\uap10.0\Microsoft.UI.Xaml.winmd"
-in "G:\cmdline\packages\was\lib\uap10.0\Microsoft.Windows.AI.ContentSafety.winmd"
-in "G:\cmdline\packages\was\lib\uap10.0\Microsoft.Windows.AI.Imaging.winmd"
-in "G:\cmdline\packages\was\lib\uap10.0\Microsoft.Windows.AI.Text.winmd"
-in "G:\cmdline\packages\was\lib\uap10.0\Microsoft.Windows.AI.winmd"
-in "G:\cmdline\packages\was\lib\uap10.0\Microsoft.Windows.ApplicationModel.Background.UniversalBGTask.winmd"
-in "G:\cmdline\packages\was\lib\uap10.0\Microsoft.Windows.ApplicationModel.Background.winmd"
-in "G:\cmdline\packages\was\lib\uap10.0\Microsoft.Windows.ApplicationModel.DynamicDependency.winmd"
-in "G:\cmdline\packages\was\lib\uap10.0\Microsoft.Windows.ApplicationModel.Resources.winmd"
-in "G:\cmdline\packages\was\lib\uap10.0\Microsoft.Windows.ApplicationModel.WindowsAppRuntime.winmd"
-in "G:\cmdline\packages\was\lib\uap10.0\Microsoft.Windows.AppLifecycle.winmd"
-in "G:\cmdline\packages\was\lib\uap10.0\Microsoft.Windows.AppNotifications.Builder.winmd"
-in "G:\cmdline\packages\was\lib\uap10.0\Microsoft.Windows.AppNotifications.winmd"
-in "G:\cmdline\packages\was\lib\uap10.0\Microsoft.Windows.BadgeNotifications.winmd"
-in "G:\cmdline\packages\was\lib\uap10.0\Microsoft.Windows.Globalization.winmd"
-in "G:\cmdline\packages\was\lib\uap10.0\Microsoft.Windows.Management.Deployment.winmd"
-in "G:\cmdline\packages\was\lib\uap10.0\Microsoft.Windows.Media.Capture.winmd"
-in "G:\cmdline\packages\was\lib\uap10.0\Microsoft.Windows.PushNotifications.winmd"
-in "G:\cmdline\packages\was\lib\uap10.0\Microsoft.Windows.Security.AccessControl.winmd"
-in "G:\cmdline\packages\was\lib\uap10.0\Microsoft.Windows.Storage.winmd"
-in "G:\cmdline\packages\was\lib\uap10.0\Microsoft.Windows.System.Power.winmd"
-in "G:\cmdline\packages\was\lib\uap10.0\Microsoft.Windows.System.winmd"
-in "G:\cmdline\packages\was\lib\uap10.0\Microsoft.Windows.Widgets.winmd"
-in "G:\cmdline\packages\was\lib\uap10.0\Microsoft.Windows.Workloads.winmd"
-in "G:\cmdline\packages\webview2\lib\Microsoft.Web.WebView2.Core.winmd"
-ref "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.AI.MachineLearning.MachineLearningContract5円.0.0.0\Windows.AI.MachineLearning.MachineLearningContract.winmd"
-ref "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.AI.MachineLearning.Preview.MachineLearningPreviewContract2円.0.0.0\Windows.AI.MachineLearning.Preview.MachineLearningPreviewContract.winmd"
-ref "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.ApplicationModel.Calls.Background.CallsBackgroundContract4円.0.0.0\Windows.ApplicationModel.Calls.Background.CallsBackgroundContract.winmd"
-ref "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.ApplicationModel.Calls.CallsPhoneContract7円.0.0.0\Windows.ApplicationModel.Calls.CallsPhoneContract.winmd"
-ref "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.ApplicationModel.Calls.CallsVoipContract5円.0.0.0\Windows.ApplicationModel.Calls.CallsVoipContract.winmd"
-ref "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.ApplicationModel.CommunicationBlocking.CommunicationBlockingContract2円.0.0.0\Windows.ApplicationModel.CommunicationBlocking.CommunicationBlockingContract.winmd"
-ref "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.ApplicationModel.SocialInfo.SocialInfoContract2円.0.0.0\Windows.ApplicationModel.SocialInfo.SocialInfoContract.winmd"
-ref "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.ApplicationModel.StartupTaskContract3円.0.0.0\Windows.ApplicationModel.StartupTaskContract.winmd"
-ref "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.Devices.Custom.CustomDeviceContract1円.0.0.0\Windows.Devices.Custom.CustomDeviceContract.winmd"
-ref "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.Devices.DevicesLowLevelContract3円.0.0.0\Windows.Devices.DevicesLowLevelContract.winmd"
-ref "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.Devices.Printers.PrintersContract1円.0.0.0\Windows.Devices.Printers.PrintersContract.winmd"
-ref "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.Devices.SmartCards.SmartCardBackgroundTriggerContract3円.0.0.0\Windows.Devices.SmartCards.SmartCardBackgroundTriggerContract.winmd"
-ref "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.Devices.SmartCards.SmartCardEmulatorContract6円.0.0.0\Windows.Devices.SmartCards.SmartCardEmulatorContract.winmd"
-ref "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.Foundation.FoundationContract4円.0.0.0\Windows.Foundation.FoundationContract.winmd"
-ref "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.Foundation.UniversalApiContract19円.0.0.0\Windows.Foundation.UniversalApiContract.winmd"
-ref "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.Gaming.XboxLive.StorageApiContract1円.0.0.0\Windows.Gaming.XboxLive.StorageApiContract.winmd"
-ref "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.Graphics.Printing3D.Printing3DContract4円.0.0.0\Windows.Graphics.Printing3D.Printing3DContract.winmd"
-ref "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.Networking.Connectivity.WwanContract3円.0.0.0\Windows.Networking.Connectivity.WwanContract.winmd"
-ref "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.Networking.Sockets.ControlChannelTriggerContract3円.0.0.0\Windows.Networking.Sockets.ControlChannelTriggerContract.winmd"
-ref "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.Security.Isolation.IsolatedWindowsEnvironmentContract5円.0.0.0\Windows.Security.Isolation.Isolatedwindowsenvironmentcontract.winmd"
-ref "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.Services.Maps.GuidanceContract3円.0.0.0\Windows.Services.Maps.GuidanceContract.winmd"
-ref "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.Services.Maps.LocalSearchContract4円.0.0.0\Windows.Services.Maps.LocalSearchContract.winmd"
-ref "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.Services.Store.StoreContract4円.0.0.0\Windows.Services.Store.StoreContract.winmd"
-ref "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.Services.TargetedContent.TargetedContentContract1円.0.0.0\Windows.Services.TargetedContent.TargetedContentContract.winmd"
-ref "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.Storage.Provider.CloudFilesContract7円.0.0.0\Windows.Storage.Provider.CloudFilesContract.winmd"
-ref "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.System.Profile.ProfileHardwareTokenContract1円.0.0.0\Windows.System.Profile.ProfileHardwareTokenContract.winmd"
-ref "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.System.Profile.ProfileRetailInfoContract1円.0.0.0\Windows.System.Profile.ProfileRetailInfoContract.winmd"
-ref "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.System.Profile.ProfileSharedModeContract2円.0.0.0\Windows.System.Profile.ProfileSharedModeContract.winmd"
-ref "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.System.Profile.SystemManufacturers.SystemManufacturersContract3円.0.0.0\Windows.System.Profile.SystemManufacturers.SystemManufacturersContract.winmd"
-ref "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.System.SystemManagementContract7円.0.0.0\Windows.System.SystemManagementContract.winmd"
-ref "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.UI.UIAutomation.UIAutomationContract2円.0.0.0\Windows.UI.UIAutomation.UIAutomationContract.winmd"
-ref "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.UI.ViewManagement.ViewManagementViewScalingContract1円.0.0.0\Windows.UI.ViewManagement.ViewManagementViewScalingContract.winmd"
-ref "C:\Program Files (x86)\Windows Kits10円\References10円.0.26100.0\Windows.UI.Xaml.Core.Direct.XamlDirectContract5円.0.0.0\Windows.UI.Xaml.Core.Direct.XamlDirectContract.winmd"
Notice that anything that I originally specified with -in is now specified with -ref, and the Windows App SDK references now all use -in.
As far as hardcoding directories, that is always a problem. As 1.8 showed, the directory structure can change, so you really have to keep on top of things. We did have a fair amount of warning since the changes happened in 1.8-experimental 2. But in the end, when using a custom build system, we kind of have to fudge things at times. The directory layouts should stay pretty stable because it is easier for Visual Studio too.
Beta Was this translation helpful? Give feedback.
All reactions
-
Ideally, I just wish I could do it with a single command or a set of simple commands, be it with a tool shipped with MSVC, with the SDK, or even some MS-maintained script I can clone. Doing it myself and being on top of things is suboptimal, and especially scary if there's some silent bad generation.
In any case, perhaps my use case is niche so I guess I need to find a way. Your comments help, thanks.
Beta Was this translation helpful? Give feedback.
All reactions
-
The problem generally is that no two build systems are really alike. This means that scripts that others write may be useless because there are assumptions or settings that do not apply to your build system. To make these scripts generally more useful, there is the option of adding options, but this can then make the scripts more problematic to use.
To give a little example, the scripts that I use uses a couple of proprietry tools. The scripts then scans for the files that it needs and then generates 7 files, 5 response files for command line tools, and 2 data files for another tool. The scripts generates these files in the aux subdirectory, and the scripts references these files from the aux subdirectory. If you wanted to use these scripts, then you would have to adhere to the directory structure of my scripts, and the file names. What is the probability that you are using the same naming? What is the probability that any modifications would be trivial? Using scripts other people write would require just as much testing as any scripts that you write, so it isn't going to be as simple as dropping it in and using it. You would also be responsible for keeping an eye on updates to that script, testing them and properly integrating them with your build system. What's worse, the author could just abandon the script and you will then be left having to deal with that situation.
A build system can require a lot of work to keep it working smoothly.
Beta Was this translation helpful? Give feedback.