Building Native Libraries for NuGet with CoApp's Powershell Tools

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
howdy my name is garrett sorak i'm a senior open source software developer in the open source technology center at microsoft today i'm going to show how to packaging native libraries using co-op's powershell tools for building nuget packages so the first things are first let's make sure that you have everything that you need in order to actually do this sort of thing uh you need windows vista or beyond um and the reason for this is because you need to run visual studio 2012 and powershell 3.0 um right now we require powershell three we'll see if we can't make this stuff work in powershell 2 later this year um but for that you need windows 7 or windows vista or windows 8. um powershell 3.0 comes installed on windows 8 by default on windows 7 or windows vista you can grab it from the download center you're going to need nuget 2.5 the 2.5 release candidate is available right now on nuget.codplex.com and go ahead and install the v6 for that which is installing the visual studio integration component uh and then you're gonna need to have the powershell the powershell tools and those are available on downloads.cob.org and if you go and grab the uh the msi there and install it it's a simple little msi no frills it won't even ask you any questions it just installs a powershell command powershell module um and then you might want something like notepad plus plus or some other text editor that's better than notepad uh and we've already got a language file for notepad plus plus so that you can get color syntax highlighting for our scripts uh which makes it significantly simpler to read so i will post those uh links when i post this video so don't worry if you can't write them down in time or that they're not clickable in the video so auto package so we created this module this powershell module and one of the one of the things we have in there is what we call auto package which is actually descended from a from an application that we wrote over the last couple of years for building packages and what we've tried to do here is simplify the packaging process quite significantly it is however written as a powershell module but don't worry if you're not a powershell user and don't and you can use this from pretty much anywhere cmd or batch files or wherever it's really not that hard the installer right now that msi is a pretty simple no frills install the one caveat with that right now is that windows installer doesn't seem to set environment variables for the current user session correctly so when you install it you either need to reboot or at the very least restart your explorer so if you log out and log back in or just kill your explorer and have it restart um that should solve it or run your power powershell session as a administrator and that seems to pick up the changes automatically um the powershell module can update itself so we've got a built-in update command built into it and the command that we're going to use today is actually called write nuget package pretty simple so we're going to be creating a new an auto package script today and auto package scripts are a property sheet based language which is uh something that's similar to css or c or things like that it's got nice curly braces so that's very familiar um it's a fairly simple format it's not complicated to learn or it's not all that convoluted or anything like that it's straightforward um but it is very developer friendly because it's not uh just it's it's not ter it's it's not overly terse but it's not overly complex let's put that away uh and it's intended to simplify automations to make it something somewhat easy and so that it's not gonna worry if you're missing a semicolon here there sometimes or stuff like that we've tried to make it as flexible as possible it does handle a ton of complexity behind the scenes so you don't have to worry about how you have to achieve certain things with visual studio or misbuild scripts and stuff like that we handle all that behind the scenes and like i said it's developer friendly there's no icky xml if you've ever tried hand editing visual studio properties uh pages or scripts you'd certainly understand why you don't want to be dealing with xml so what are we going to package today um i have downloaded the windows the c plus plus sdk for the c4 plus rest sdk which is available on codeplex and someone asked me to actually go ahead and package this so i figured i'd grab that and so what i've done here is let me just switch to my other monitor um i've actually unpacked it and i've it comes in three parts it comes in the microsoft c plus rest sdk for visual studio 2010 it has one for 2012 and then it has the c plus plus rest sdk for the windows uh store apps and we can actually look in you know we can look inside of here and you can see it's got a bunch of uh components but let's just do you can see there's dlls for a bunch of different a bunch of different scenarios arm x64 x86 there's debug there's retail there's some for the 2010 2012 a wide variety of different things in here um as well as there are the lib files and all the header files and everything else that's in there and so there's a lot of files in there but we can make it pretty easy for us to uh to go ahead and suck all this stuff in there so let's just get back to here and move along so we're gonna do the c flip plus rest sdk so packaging libraries on windows the first thing we actually need to do is actually start creating our auto package script and we need to start with the metadata which is basic information like the name the version links the project license description summary icon etc let's uh just go ahead and use our notepad plus plus and we're going to call this the c plus plus rest sdk dot auto package yeah and yes we want to create a whole new file and so what i'm going to do here is i'm going to try to pick the stuff that i've produced ahead of time but it's really simple to play along so uh to start with we need uh in our new get package script we need to have a little get a new get node which tells us all the things that we're going to put in our nuget package and then we have the content of what's called the new spec file and if you've ever used nuget for managed libraries you know they have to create a new get uh you have to create a new spec file which contains all the metadata and stuff like that now you have to create in using the default tools you have to create an xml but here it's going to be a lot simpler than that we're just going to go ahead and actually list all the properties and whatnot that we want to want to stick in there and so i'm just going to copy and paste this from my my other one and essentially all we have to do is put in all the different uh different values we have an id we have a version we have a title uh we have the authors and this is a uh you can list all the different authors that are in this one the c plus plus rest sdk was maybe just by some people microsoft so we're just going to slap microsoft corporation on that owners we're gonna slap we're gonna put whoever's owning this project pack or this package itself so that when somebody goes looking for who packaged it they can find us and so i've got one for the co-op project i've also said you know my name so if somebody's looking to find out hey who packaged this and what can we do to fix this or whatever the license url you can just list it there the project url and when you've got just plain strings in here and there's nothing overly complex you can actually just uh list them without the quotes that quotes are are optional in almost all cases um we have an i uh uh a url to an icon and this allows for that packaging time or when the package is showing itself in the ui you can actually see the url the icon itself and so we found a url online for that um whether you want someone to be forced to accept the license before they can use it um and then you have a summary of text which is basically the what it's all about and then you also want to supply a description and the description if you wanted to span more than one line just stick an ad symbol in front of the in in front of a quote and put the whole thing in quotes um and it's just like c sharp where you have that symbol uh litter string literals anyway you can just list the whole thing there and so i've got the all the text in there blah blah blah you can release you can put some release notes about this particular version copyright thing and then you want to tag it and make sure that when you're making native apps or native packages that you actually tag everything as native uh put a native tag in there because that'll help people when they're searching for their stuff later you know what we'll also do is we'll put in um which is what they called their code name for for this uh for the c plus plus rest sdk so there's there's the basic new spec data and you know that would be very very similar to the stuff that you have to put in the xml file um if you're dealing with it that way so let's stop and go back to our little view over here for a second and let's talk about about the complexities of building packages um in c plus plus far more than any and then things like dot net we have a lot of different variations that we have to deal with and neither nougat direct nougat can't really directly handle all the different variations and even so and even worse the the ui and visual studio doesn't really help us out in dealing with all these variations um and almost all c and c plus plus libraries that you have or have all these many flavors so there's things like platform you know changing the platform from x86 to x64 to arm different releases different configurations like debug and release different tool sets vc11 vc10 you know even go backwards vc9 some people are still using vcc vc6 for some things um and with a little bit effort we may be able to even get gcc involved one day um different kinds of linkage right we've got dynamic linking which is very popular static linking which is good for other things link time compiled generation which is good for using pgo compiled to do performance enhancements and side by side so there's many different ways to look at things like linkage and stuff like that calling convention although a little bit more rare and hopefully people aren't using calling convention as much as they think they should um sometimes libraries package this standard call or cdec or both some are available both depending on what the consumer was using application types we've got windows 8 windows 8 phone apps we've got desktop apps i'm sure that this list is not not shrinking it's actually growing and then some libraries have different things for different character sets so some are utf-8 16 unicode this is getting pretty big now the worst part about all this stuff is that i've listed you know seven different kinds of things that you can pivot on and not everything that you could pivot on is in this list i don't even know what they all are sometimes i'm gonna come across a library and it's gonna be oh we have one for red and one for green and it's like what does that mean and so the great part about the way that auto package works is you don't have to know every different kind of combination that exists for everything you just have to know what your library needs to be able to support and using that those different things you can create different what we call pivots or configurations that says that this one is for this particular combination this one's for this particular combination uh and you can set all these features and whatnot to create those things now don't worry it doesn't have to be all that terribly complex um it's actually pretty simple we're gonna actually let the tool do all the hard work and it's actually pretty simple what we're just gonna do is we're just gonna list the things and put it in the right place and then we're gonna tag items with a condition and we're gonna let the tool sorted out so let me start by switching back to our edit window over here and so there's our our nuget package and so the first thing i'm going to do is we're going to create a new files node and this is basically all the content that's going to go into the package and by default when you put something in the right spot according to the right thingy it will deal with it and put it put it in the right place in the package and it will make sure that it gets sorted out when it gets installed and and hooked up correctly and examples of this are if i'm going to put things in the include folder or if i'm going to put things in a in a bin folder or a lib folder i want to make sure that everything gets handled correctly so i got a whole bunch of files on disk we saw those before uh and i've got some i've got some commonly used directories and so what i'm going to do is just actually list a few what i call defines and we can just define little macros that we can use reuse later so that we can find all of our things now i've basically got three different three different models of stuff that i want to be able to dig out stuff from the windows rt or the the rest sdk for the for for windows 8 apps um i've got stuff from the 2010 sdk and 2012 sdk so that's pretty easy so the first thing i'm going to do is uh i'm going to get all of our include files and i went looked and on on disk and all of all the include files are the same there's two sets of include files and they're essentially exactly the same so i'm only going to need to include the one the one set and so i start by saying oh include this uh these files and so it's you can see here i've got a macro that's resolving to this so i could just list this straight away and it would just work exactly the same or it's just going to pick up the value for my define and actually use it and inside there there's a directory called include and basically i'm saying pick up everything from there and bring it over and it will bring it over and make sure that it's all kept inside there if i wanted to do everything in all of its sub directories as well i can use the double star which says all the subdirectories and whatnot but i know that the contents of that directory are just simply one deep so the great part about this is that anything i stick in this include section is going to automatically get um placed in the right directory and the include directory itself is going to get added to the uh to the right and right spot in visual studio so that everything just finds itself automatically and if you want to put more than one item you can certainly do that you can actually just list and say i had food.h here or if i had um you know you can list whole paths to something uh food.h or whatever uh and there's something you can just separate things by commas and find them if you have if you want to you can put them in quotes it's not necessary the only time would be necessary is if you have a comma or a semicolon in the file name itself i hope you don't do that that'd be kind of weird anyway so this thing here we've got this this collection of stuff that goes in the include file easy as pie the next thing that we're going to drop in there is uh documentation and so there's documents that you want to ship in there you can just put them in the docs collection and it will let you just uh it'll pick up those files and drop them in there and so in this one particular case look at this i didn't even uh i didn't oh because i didn't use the full path i just used part of the path so you can see here i've just given it this thing and it's okay to have spaces on the outside of things that trims them off on the inside it makes sure that the space are there so it knows where to find those things um it's quite space insensitive outside of the outside of strings and stuff like that so don't worry about that so this i mean i could put this you know i could format this the way i like to do stuff or i could just leave it essentially ah let's just leave that back where it was and move along okay so now here's where it gets a little bit complicated i've got different library files for different uh combinations of of things that i'm going to use and without getting into too deep of the too deeper detail this time i want to show just do this in a simple way i can basically say i have got uh a collection of things that i want to scope to only when where it's x64 uh v100 which is the tool set which is the vc10 and when it's a debug and these can these are keying off of actual platform values inside the inside the visual studio project system um and i can show you in a subsequent video how we can actually use these to our advantage to do an awful lot more and even define our own uh so it all just works nice and natural like so what i'm going to do is i've got that and then inside of there i can actually declare a few more connect collections of things and so i've got a lib so in the same way that i've got include and docs i can say put things in a lib directory and what happens is that it's actually going to i'm saying grab the files from here and it's only one file so i've actually emitted the the braces but if you want you can actually put the base around that it's no big deal if you've got more than one you're going to need the braces if you've only got the one uh though the braces are optional on the collection um basically it's going to go and grab this file and it's going to stick this into the lib directory but it's only going to stick in the lib directory when you are on x64 vc10 and debug and we can also specify things like symbols where it's going to grab the the pdb file and what it's going to do it's going to create a special symbols package that we can upload to a symbol server later and then we can say oh well we also need to have a dll file and we need to stick that in the bin directory and the contents of the bin directory get all the files that we're sticking here get copied to the output directory when we actually built and so that happens all automatically you don't have to do any of the hard work to make that happen and so let me just go and grab a few more of these conditions here so let's grab these ones so here's the rest of the vc10 uh ways of pivoting on this stuff i've got the same three directories here libs symbols and bin lib symbols and bin lib sybils and bin um all i'm doing here is i'm changing the things that i'm pivoting on right this one's for x64 v100 release this one's x86 v100 debug x86 v100 release so these are the different sets or different pivots that i want to make work and all i have to do is say when it's this one use this file when it's this one use this file and it's this one use this file and the rest of it is going to be handled 100 automatically uh the next set of things we can do is the same essentially the same different the same sets but for um but for vc11 and so what i'm going to do here is i drop these ones in and so this one is vc11 this is x64 v110 is the is the vc11 debug and now i've added another pivot actually which is called desktop and so this is for non-windows rt apps this is for the windows desktop apps and so what i've done is i've provided the the libraries the pdbs the dlls for all of these things and the reason i'm going to say desktop here and i don't say desktop here is because desktop is not really an option with vce10 it's just all that all those apps are going to be desktop whereas under vc11 we actually have desktop or winrt apps and so we'll come back and and let's take a look at how we can do the pivoted versions for windows rt and in windows rt we've got a whole bunch more sections here let's just uh stick these in and so they start right about here where we've got arm v110 debug rt and then we've got x64 if you want to know debug binary x86 v110 debug t blah blah blah so this is basically all the different variants of things that we can that we need to be able to handle um and we stick all these things in there and it will sort out which files get included where so the question is what else do we have to do um actually this is this is the actual bare minimum of what we need to do i'm going to add one more thing here which is after the files um declaration altogether and we're gonna under enter in a target section and targets is uh there are two files that get generated when the auto package builds the there's sorry there's three packages that get generated when auto package does all of its work in each one of those packages there are multiple files that get generated and one of which is a dot targets file which is a visual studio project file and one's a dot props file um and depending on what you're doing and and what you're needing sometimes you need to be able to add a few more features in there or you'll be able to set some stuff in the targets file and essentially we can actually go and add additional items into the into the build and we'll talk more about this in exactly how this sort of thing works and whatnot and in later tutorials but right now we can just add something to the defines which means it's going to add it to a define the the predefined things that the compiler sees when we start we can also actually scope these kinds of items to x86 you know i can say that i can say defines plus equals you know is x86 today and it will make sure that that gets defined but only when the project is being built for x86 if it's being built for x64 or anything else it doesn't have to um and in another tutorial we'll talk more about how the how all these con different pivots and configurations and conditions work uh there's a huge amount of work behind the scenes that can let you do some very complex things but today we're gonna make it really simple but anyway we're just gonna actually flag one little thing that says has rest sdk if we uh we're hoping to actually get a standard about these sorts of things so that software can be a little bit better about picking up what it needs okay so producing the outputs let's actually go back to our powerpoint for a moment talk about this so the things that we're going to actually produce um is we're going to actually produce three packages from this we're going to produce a main package and that's what's going to have the library files in it the like the things you link to it's going to have the header files in it and it's going to have uh the documentation and and whatnot in there uh and then it will have a link actually in that it'll have a dependency to the redist package the redistributable that has just the binary so the dlls in there so if the package just needs the outputs from some other package it can grab that one uh and then we're also going to grab a we're also going to generate a symbols package here which contains the debug symbols so that if somebody wants to debug your application they can get them that way and the great part about this is by automatically generating this symbols package we're going to be able to upload these to a symbol server on the internet so if someone's trying to use your software and you're trying to debug something from your package um you can actually have the symbols online so that they can just grab them easily just as easy if it was from somebody else okay command prompt here and we'll actually build our package and so if we look in our directory here we have our cppress sdk dot auto package file that we saved and so all we have to do is go right to nuget package and give it that actual file c plus plus cpu rest has to get over the package file and hit enter and it's going to go off and actually produce all the files that it needs to do and it's going to then spit out these different new page files which contains the actual which are the actual packages um and update files are kind of zip files so we can actually take a look in the inside of those if we want to and you can actually see you know inside the package itself there you can see all the header files got stuffed in there we have some props files and some targets files and these are what tells visual studio how to use these things there's also an xml file in there which allows us to manipulate some of the uh other settings and we'll talk about those in a later in a later tutorial as well uh plus you can actually see all the lib files for all the different variants and all the different different stuff in there and these got placed in different directories according to where it needed to be um so then we see so we see these things here and then what i'm going to do is i'm just going to copy these uh these um files to my local package repository which i set up previously and so i've got those in there and now we can actually talk about consuming these things and so consuming the package is actually pretty darn simple from any project you can actually just go to click on manage nuget references you choose your package and start coding so why don't we try that let's jump over back to our visual studio over here and uh let's start visual studio whoops sure we'll run into administrator why not awesome sauce okay so let me go and make a new project here and i'm just going to make a standard win32 console application and uh yeah we'll just call it uh test app but like a better word and just go ahead and do whatever it needs to do so we're going to start with this essentially blank console application so what you want to do is go over to your project your solution explorer and you right click on your on your project and you'll see this manage nuget packages in there it's very important that if you don't have two nougat 2.5 you won't see this for c plus plus apps you'll only see it on on manage shops so for c plus plus you're going to need to have that nuget 2.5 so click on manage new get references and then you go to the online section and it will search different package sources and so you can add or set things and and i'll let you know others describe how nuget works and whatnot um i'm just going to choose things from my local package store so here's the c plus plus rest sdk that i i'd created there's that icon and it downloaded that off the net uh you can see here that it says created by microsoft corporation id id the version the description and this is all the stuff that we put into that uh into that auto package script and all went into here and so now we can actually see all of this stuff um and so what we're going to do is we're going to just grab the c plus plus rest sdk if there are dependencies it needs it will grab them all and so on so we hit install on that and you can see that by virtue of this one go lighting up as well it grab the redist which is obviously necessary if we're going to code against this so we'll just code that close that and then in another window here i've actually got a uh i've got the source from one of the samples from the c plus plus sdk so let me just paste this in directly and so the great part is is that you can actually see that it's not even complaining about things like http client file stream which are actually from the uh from the header files from the actual uh package that we just installed if you go to external dependencies you can actually see that let's see what was one of them called http client it's in here somewhere come on try it again http http client as well it's right there so the great part about this is we did not have to set the include directory we did not have to set any of that up that actually all came with the package and just automatically set itself up for us and it set it up for the condition that we're going to compile under so right now we're doing win32 and we're doing debug if we wanted to do win32 and say release or if we want to do x64 and release or if we wanted to do you know whatever combination of things we're doing it's taking those values into account as well so if you've got special includes that only work for you know one one pivot or one configuration over another that's great but in this particular case let's actually just go ahead and build um oh and did i delete the standard header file yes i did my fault there we go you're supposed to leave that in there and we hit build and of course we're going to hear my nice little oh there's that that's my that's my happy sound the uh thing finished building okay so then it's finished and uh i believe that if we went to uh root temp and we called it test app we can actually see the uh if we run the debug directory we can see here's our exe where is it there's our testapp.exe and there's our casablanca 110.dll so it made sure that the the dll that we needed got pulled in and brought over here and that happened because if we look back in our in our thing here where is uh we're using v110 desktop x86 x86 desktop debug so it made sure that this dll file ended up in our build directory after it was finished it linked with this library file and then it's the symbol files are still in the symbol thingy because we have an extra we don't have to extract those um anyway so then we can actually go ahead and run this so if we go test app and of course it's going to say bing request well it's because that's what it says in the in the printf inside there but if we go test app and then says search terms so let's go uh co-app and then let's go output files let's go test.html oh response code 200 returned so if we do to dir well there's the test.html whoops there's the test.html uh so let's see what that looks like uh and there it is that's the search results that we got back and you can actually see it's pulling this from the file that we actually grabbed so this is really great news so now we've actually managed to build compile link and uh run an application all without ever having to look at the visual studio project files all without having to say even modify any of the properties by hand now without having to specify in the source directory not having to specify library files not having to specify any of that stuff we just went and and relied on the contents in the package ahead of us or given to us and it worked just fine so if you're kind of curious as to how that all works we can actually go back and if you look here you can find the packages directory so let's go into packages uh and there it is there's our c plus rest sdk so let's go into that folder and this is the package that got on that got downloaded and then packaged unpacked for us and all the stuff in there and inside there's a build directory inside the build directory is a native directory and inside there you will see a couple of you'll see some files in there you'll see a c plus cpp rest sdk.targets one.props and then a properties ui file so we look at the the the props file or the let's actually look at the the targets file so c plus plus rest sdk targets so here is we'll just turn this into xml so that everybody can read it i'm not sure that was an improvement holy cow those colors are i'm not a i'm a new user of notepad plus plus i've been using other editors for a while so forgive me uh anyway this is uh this is a visual studio project this is what got generated this is what auto package did all the heavy lifting to make work uh and what it went did was make sure that all the different properties and all the different uh all the different pivots that we handled are doing all the right things at all the right places and so you'll see things like item definition groups and you'll see all these conditions and oh my goodness this stuff is overly complex oh if it's arm and the platform tool set is v110 and configuration is debugged and the target type is winrt holy cow because this getting more complex well the great news is you don't have to know how this works you just have to actually use it and and deal with it and we've handled the other so there's a bit of complexity in how it generates some of these properties and how it actually makes use of these things if we had things for linkage for like dynamic or static or stuff like that or calling convention it actually handles generating properties for those as well we don't need that in this particular thing but it generated all the stuff that wouldn't be necessary to make that happen um and so it basically has made sure all these things have there's our preprocessor definition cp has cpp rest sdk that we put in there uh you'll notice this item definition group doesn't have a condition so it happens all of the time uh plus the include directories that have got pushed into here and made sure that we had a reference back to the include directory so that that's how our our header files got automatically picked up but generally looking at it this is actually you know there was a hand creating these is very problematic and painful and to make sure that works under so many different conditions just gets to be a nightmare anyway uh so that you can actually see is those things and then there's also a um you can see the docs themselves are in here actually if they go like that of course not how can you tell that i know i'm a i'm an old schooler at heart you can see all the header files are in here and all the styles all the files that we need for all these different conditions and stuff like that and it keeps track of this stuff you don't have to worry about keeping track of you don't have to worry about how this all works together you just need to use it so that is as they say that so let's actually jump back to our little powerpoint over here and we can talk about a little bit more before we're done so what's next um so this is this is an introduction to how we're actually generating packages and whatnot i hope you've had a good enough time i wanted to keep it as short as possible without having to go into too much detail um upcoming tutorials we talked about how to upload packages and stuff like that uh deep dive into exactly how it works and where those things go and how these things get built uh creating and using dependencies and customizing those targets and props files further so if there's additional things you want to have happen in your package we can make those things so we can show how to to do a large and wide variety of interesting things with that uh and we'll take a look at some advanced advanced scenarios yes um and so past that i would like to say thank you for watching and uh please follow me on twitter at fear the cowboy or watch my blog at fearthecowboy.com and come visit the co-op project at coop.org i'd like to thank you for coming out and have a great day you
Info
Channel: Garrett Serack
Views: 6,088
Rating: 4.875 out of 5
Keywords: NuGet CoApp
Id: l4MAkR13JPA
Channel Id: undefined
Length: 33min 17sec (1997 seconds)
Published: Thu Apr 18 2013
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.