Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

What Makefiles do

by throop (Chaplain)
on Nov 08, 2006 at 17:17 UTC ( [id://582930]=perlquestion: print w/replies, xml ) Need Help??

throop has asked for the wisdom of the Perl Monks concerning the following question:

Brethern,
I can't quite understand what Makefiles do for Perl modules. The camel book states
If you don't know what a Makefile is, or what the make(1) program does with one, you really shouldn't be reading this section.
Which is way helpful. Anyways, I thought knew what make and Makefiles do for C and C++ projects — check source file dependencies and binary files, seeing what needs to be recompiled.

But I didn't think these Perl modules were pre-compiled. I thought the compiler gets invoked at run time. I don't see any binary files generated. (I realize that some modules include C code that does get compiled, but I'm not talking about those cases.)

If I get a CPAN module which only contains one .pl file, why do I need a Makefile along with it? Why wouldn't it be enough just installing the .pl file in a directory, and making sure that directory is in @INC?

Replies are listed 'Best First'.
Re: What Makefiles do
by j3 (Friar) on Nov 08, 2006 at 17:48 UTC
    If I get a CPAN module which only contains one .pl file, why do I need a Makefile along with it? Why wouldn't it be enough just installing the .pl file in a directory, and making sure that directory is in @INC?

    Usually, you use make to do 3 things: build, test, and install. In the case where there's only .p[lm] files involved, there's probably no build necessary, but the makefile can still test and install.

    After running perl Makefile.PL for a given pure Perl module, you might check out the resulting makefile to see what work it's doing for those abovementioned tasks.

Re: What Makefiles do
by blue_cowdawg (Monsignor) on Nov 08, 2006 at 17:29 UTC
        I thought knew what make and Makefiles do for C and C++ projects

    I'm confused... why would you think they'd be any different in relation to module distributions? It's exactly the same function.

    Normally CPAN modules come with a Makefile.PL file (or more recently Build.PL, but that's another topic) which you run the Perl interpreter against to create a Makefile which then allows you to run:

    $ make {spew gets issued} $ make test {test result spew here} $ make install {installation spew goes here}

    Now, I've made a huge assumption here that you're on some flavor of *nix which I realize is a bad assumption. If you're on some flavor of *dows YMMV.

          If I get a CPAN module which only contains one .pl file, why do I need a Makefile along with it? Why wouldn't it be enough just installing the .pl file in a directory, and making sure that directory is in @INC?

    That didn't quite make sense to me. The CPAN modules I've seen come with (as I said above) Makefile.PL files and/or Build.PL files. Either way if you run Perl against the Makefile.PL file it will "do the right thing®" while building the Makefile for the particular system you are on and the way the Perl interpreter on your system was built.


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: What Makefiles do
by Fletch (Bishop) on Nov 08, 2006 at 17:40 UTC

    Yes, and that's exactly what the Makefile does: moves things around to the correct place. The entire set of plumbing is there that knows how to take XS and compile it into a loadable module but it's not used; it's just the "move .pm files to the right place" targets that are run in that case.

Re: What Makefiles do
by brig (Scribe) on Nov 09, 2006 at 00:59 UTC

    Please forgive my diversion...

    make can do anything you tell it to do, most specifically execute programs or entire pipelines.

    The magic is that it does so based on a hierarchy of dependencies between files.

    I actually use make for building static (non-DB) based websites. I edit any content that needs changing, type: 'make sitename' and whiz bang presto magico the make file does everything else (I mean everything) and makes sure it worked. If it doesn't work it even sends me an email to complain since sometimes the update can take awhile and I change subjects...

    I bet you never thought of make as a CMS? didja?

    The point being of course that it is not about make, it is about abstracting the build process (even if there isn't one perse) so that you can assemble, move, test, and complain on the fly. Just because one particular module doesnt really need it does not mean that the technique is broken or that it should be left out of a simple module.

    Consistency is next to cleanliness

    Need a good laugh? type make love at a unix prompt (doesn't work on Darwin)

    Love,
    --Brig

Re: What Makefiles do
by jgamble (Pilgrim) on Nov 08, 2006 at 20:26 UTC

    Just to add to everyone else's comments: You probably have "make" and "compile" joined in your mind, and that's not unreasonable given make's history.

    But if you look at make's documentation, you'll find that it can run virtually any command that you can on the command line, which makes it extremely useful when, for example, you want to create a package to load up to CPAN. You could use it to do something as trivial as display the current time, or you could use it to create graphics packages. The commands in the makefile are up to you.

    By the way, having said that, check out Module::Build, which makes some of the Makefile configuring much easier.

    Edit: Added missing "have" to the text. Sorry about that.

Re: What Makefiles do
by bunnyman (Hermit) on Nov 08, 2006 at 20:25 UTC

    If I get a CPAN module which only contains one .pl file, why do I need a Makefile along with it? Why wouldn't it be enough just installing the .pl file in a directory, and making sure that directory is in @INC?

    I think you mean to say .pm file. You can just copy a .pm to some directory in @INC and it will work. That is what I do because my company doesn't keep Perl modules in a place that Perl knows anything about.

    If the module has C code in there, this won't work, but you already said you aren't talking about those modules.

      That is what I do because my company doesn't keep Perl modules in a place that Perl knows anything about.

      If you provide the PREFIX variable to the perl Makefile.PL command you can install a module anywhere you want using the standard process. e.g

      perl Makefile.PL PREFIX=/my/custom/INC/dir/ make make test make install

      This hasn't always worked exactly as planned but it has worked for me for many years. I never install modules into the OS perl distribution and always install modules in my home directory when I'm on a system where I can't install another perl distribution or don't have root priveleges.

      Refer to the perlmodinstall man/perldoc page http://perldoc.perl.org/perlmodinstall.html

      CPAN and CPANPLUS can be configured to use this PREFIX by default so that you can install modules in your special place as easily as into the distribution. Just refer to their documentation.

      --
      Clayton

        I am paranoid about running a makefile that I did not write and know nothing about do something to an important directory. I would rather set PREFIX=/tmp/sandbox and then go check in sandbox and copy the files to /net/production/perl/lib myself.

        The point I want to make is that Perl does not really care how the files got there, so long as @INC is set correctly and the files are in there.

        when I'm on a system where I can't install another perl distribution or don't have root privileges

        why talk about root. A user lile perlinstall is fine. I am actually paranoid about doing make install as root...especially when you don't need it.

Re: What Makefiles do
by Firefly258 (Beadle) on Nov 09, 2006 at 03:14 UTC
    I thought knew what make and Makefiles do for C and C++ projects — check source file dependencies and binary files, seeing what needs to be recompiled.

    I think you meant "compiled" up there not "recompiled". Makefiles are similar to shell scripts, a series of instructions for the machine-local copy of make to process for different stages of preprocessing, compiling and installing packages from source. And the very same applies to perl modules too.

    You have to remember that perl is a high level language whose capabilities transcend the different non-portable languages built for dedicated platforms and architectures (like C, C++, etc). This makes perl really powerful and portable but at a cost, the layer of abstraction at which perl operates doesn't allow a perl user to directly access the system, create data structures using the fundamental data types, interact with hardware and external libraries, etc. One of the ways perl modules get to work identically across the various host environments is to initially build a "platform" (for lack of a better word) that is dependant on the machine's subtleties - C compilers, fundamental data-types, endianness, memory management algorithms, available libraries, paths, etc, etc. So it is the job of makefiles (in perl too) to analyze the system, create rules, compile C code (in XS/SWIG) and install the compiled binaries into the perl path (@INC), etc. The importance of the makefile in a perl module varies - some perl modules contain very little perl code and are largely comprised of compiled XS, other's contain pure perl and little or no XS.
Re: What Makefiles do
by NovMonk (Chaplain) on Nov 09, 2006 at 13:39 UTC
    I was first introduced to makefiles as kind of "batch files on steriods." My old company used them to execute market research tabulation runs-- instead of having to type commands manaually, you put them all in a makefile, and you could put variables at the top of the makefile for job name, date, everything. then just type "make tables" and go get a cup of coffee.....

    Cargo cult at its best (worst?) And it made it a little confusing for me later to get a handle on what makefiles are Usually for.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://582930]
Approved by BaldPenguin
Front-paged by bart
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-04-24 03:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found