Streamline Projects with Makefile

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up guys yankee here and in this video we will be talking about how we can streamline projects using makefile so with make file we can streamline our projects by managing dependencies triggering bills and distributions lending and formatting running test and many more you can do a lot of things using make and make file now to see makefile in action let's say i'm recently on boarded to this project but i do not know how to run the initial setup i do not know how to run the test and any of those things and my colleagues are busy with a production bug so they cannot really help me right now and they just tell me to run the make command and you'll know what to do so that's what i do and boom i can easily go through each one of them and figure out what i need to do right now since i am starting the project fresh i need to install the dependencies and it says use the install command so what i can do is activate my virtual environment and do make install and it installs all the dependencies i did not have to ask my colleague what commands to run or whatever there is a standard set of commands there it runs it so let's see other commands that are available so there are check clean compile and other things let's say i recently added some content to a file now i want to see if it is compatible with the black format or not so i'm just going to do make check and it shows all the changes that i need to make on a file called setup.pi so now that we have seen make in action next we will see how we can create our own make file for our project so let's get started if you are on a linux or apple machine then you should have make already installed on your device you can check by using make minus minus help so if it shows you this health message then you have make installed and if you are on a windows machine then there are some loops and hoops that you need to go through to install it so i'll also be leaving the instruction on the description section below so right now i'm in my project directory uh if you want to follow along you can find the repository link on the description section below so first of all i'm going to create a file called make file because every make command needs a make file so make file follows the yaml syntax so whatever you will be writing it should follow the yaml syntax so before we write anything on it i want to showcase the basic anatomy of a make command so what makes a make command right so there are three parts to it the first part is called target the second is called dependency and the third is called a recipe and as a whole this command or this you know part is called a goal because with make command you are trying to accomplish some sort of goal here so target is whatever sub command that we run after make right so right now the name of the sub command is target so if we do make target it is going to run this recipe and what is dependency then so dependency is something that our target depends upon to run so let's say we want to showcase hello world before we run hello how are you so we mention the dependency on the target here so what it does is whenever we run make target it's going to run dependency 1 so depend 1 first then it is going to run its own recipe so let's see that in action if i do make target then yeah it runs hello world first followed by hello how are you now let's get building make file for our project i'm going to delete this example for this tutorial i'm using a python project but you can virtually use make file in any project of any language so yeah uh let's first start with writing our first uh target it will be called install and i'm just going to write a command it come handy later on so i'll say install uh depend nc right like i said it is a yaml file so i'm going to have an indentation there and what it is going to do is just keep call minus e in it for editable mode and dot to install all the dependencies of setup.pi file uh we are going to be installing dependencies using this command now let's see if it works first let me make a virtual environment make install and as you can see it is running ppinstall minus e and installing all the dependencies that are mentioned in setup.pi file but let's say i do not want to showcase the command that i'm running then you can just put the add the rate symbol on the front and it will just mute it so next time i do make install it's not going to so anything it's just going to output the logs that comes by default so now we have a single command ready in our make file next i'm going to create a target called check and it is going to check for consist then consists since he's using black formatter right so again uh indentation so i'm going to suppress all the commands because i do not like it going up in my terminal but your uses may vary so for this what i'm going to do is run black check and it's going to be if dot so once i have this command in place i'm going to save the file again and kick if the check command runs to make check and it works uh let me create a few more uh just to see the example of what we can do so test is going to be right is nice yes and like check i'm going to add a one call format format going to format everything uh that has been sold by this command so yeah format all the these right so within few seconds or within few minutes we have a few workable commands that we can put in our repository and our team members can use so let's check out our new recipes so if i do make format so the file has been formatted and if i do make check again then there is nothing left to format because it has formatted it if i do just make it does not show me the documentation that we saw earlier so it requires some custom scripting right and for that i'm going to pull out a command called help right so this is the command that enables enables us to show the health message using just the make command so if i do make help then it's going to show all the targets that we have like check format help install and test right but if we do make it again runs the install command because by default make runs the first target it encounters so what we can do is either put this help command on top of the file or we can just you know specify a default poll and which is going to be help so now if i do make it's going to show me the health message next i am going to show you how you can include multiple make files into a single file so this is the composition feature of make file where you can have distributed make files all over your project doing different things and you can pull it all together onto a single file and use it so i have a make file inside of my docs directory and i'm going to pull out the content from this make file to this make file so to do that all i have to do is include and run a cell variable called pwd right and it's under docs and make file so now that i have included the make file inside of the docs directory now i can use the commands that are listed here so it has docs and served docs and a few things about variables in make file so right now you saw me use the pwd environment variable so you can actually import all of the cell variables that are available on your terminal so if i do echo dollar pwd then it's going to give me the directory where i'm currently located the docs directory is also inside python project docs and make files so you can make use of the environment variables that are in your terminal and use them directly on the makefile so that's a pretty neat feature or you can create a variable yourself as well like we are going to do right now so i am going to create a variable called pip install right i'm going to create a variable called pip install and what it's going to do is keep install minus r so wherever i'm using peep install minus r i can replace that command with just keep install so i have just a single place that has a beep install minus r command used or maybe i can use another one here as well so let's see minus r and inside of my requirements folder i'm going to go save.txt right so i have a requirements folder and dave.txt has all the dependencies i'm going to install that along with the dependency in setup.pi so i can literally remove this and use the environment variable use dollar followed by curly braces and right click install and importing this file into this file the variable i have defined here actually works in this file as well so that's pretty neat now if i do make again then it is going to you know include the docs right from here and also the serve docs command now i can run make docs to install all the docs related dependencies so now we have this issue how can we solve it what make is trying to do is when we run make docs uh make usually tries to create a file or a folder uh with the name of the target and in our project directory we already have a directory called docs right so what we can do is mention a phony target so phony is going to be and what i can do now is make docs and it's going to run perfectly so phony targets are something you need to be aware about because if you have a file that is named similar to your target then it is going to have a conflict and you need to mention them in the phony section here now i'm going to do make serve docs right so i'm going to have our documentation site that is being served to me with a single command i didn't have to ask my colleagues or anything so i'm going to stop the server next up what we are going to see is kind of a weird example because i'm going to create a golang file here so i'm going to call it app.go so what we are going to see within this example is how to build you know binary for multiple operating systems so one for linux one for windows and one for apple computers right so for that i'm going to write a simple golang package uh pack is main punk and it is very very simple it's going to so hello world and nothing more so i want to compile this file you know into binary that is supported in three different platforms so i come to my make file right so what i'm going to do is create a command called compile linear or i can just call it compile so that it you know compiles all the three binaries at a single time so next i can do this make compile and already has a bin directory here and boom i mean it has all three binaries built for me within seconds so you don't have to enter each commands one by one another cool feature about make is that you can run parallel processes or parallel targets at the same time using the minus z flag before that i'm going to you know put the name as linear again and i'm going to paste some commands here so what i'm doing is i'm breaking the compilation command to different targets for different operating systems and i'm going to run them using a single command and what i'm going to do is create three jobs or it is going to use three processes sorry three processors to compile all of these three binaries so let's see how it works so specifying dollar make actually runs a recursion that is we are running make command using a make command so to do that we just have to specify this make variable here and it is simply like make minus z preprocessors compile windows right this one compile linux and compile for os x i go to my terminal and i will also like to pull out the resource monitor to see the cpu uses when i actually run this parallel command you know compile so let me pull that up going to have a small section here right apparently it's sitting around you know 30 25 to 30 percent and if i do make compile going to rise significantly so yep rose up to 33 percent and down and it already has these things made up or if you want to see again i'm going to delete this right and i'm going to compile it one more time and again like 39 and we have all these binaries compiled for us so that's pretty neat although we have two commands for compiling the binary one is called compile linear and one is just compile uh where we are using multiple processors to actually compile this binary uh the linear compile is actually faster so i showed you this example just to show that it is possible but it is not actually required here so i'm just going to delete it for now so guys that is all for this video i hope you learned something new about make and make file and how to streamline your project give me a thumbs up if this video was helpful and comment down below if you have any queries and make sure you subscribe to my channel i'll be coming out with more contents in the future so till next time bye
Info
Channel: Yankee Maharjan
Views: 549
Rating: undefined out of 5
Keywords: makefile, gnu make, make automation, make python, makfile python, makefile go, make go, makefile golang
Id: vybdPTfNLo4
Channel Id: undefined
Length: 14min 6sec (846 seconds)
Published: Sat May 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.