Quote:
Originally Posted by hazel I might just take you up on that offer. "Cinderella" is also pretty simple: 2 small code files and a header file. The only libraries it uses are glib/gdk/gtk and of course glibc. At present I have a simple makefile which I wrote myself and which works on my system but I don't know if it would more generally. For example I don't know if the libraries are always in the same place in different distros - my makefile uses pkg-config to find them. This is supposed to be the kind of thing that autoconf looks after in a proper package but I can't see what information I need to give it.
I've had a lot of help from the community over the years. I just want to be able to give something back. |
Well, I couldn't find a terrible amount of tips on how to write a makefile, so I left most of the work up to a config script written in bash.
Code:
#!/bin/bash
findlib()
{
whereis $1 | sed "s/$1: //g" | sed "s/ /\n/g" | while read file; do if [ -f "$file" ]; then ls "$file"; else return 1; fi; done
}
#whereis returns locations of file searched for. sed parses this result so that the while read loop can parse it, test if the file is okay, and then list the location of said file. If [ -f "$file" ] fails for some reason, it then returns 1 for exit code
echo -e "Checking dependencies...\n"
echo -n gcc && gcc --version > /dev/null
#first print gcc, then run gcc --version to see if it is installed
if [ $? != 0 ]; then
echo "gcc not found"
exit 0
fi
#report gcc not found if exit code of gcc --version does not equal 0
echo -e "\tOK\n"
findlib libc > /dev/null
#run findlib for libc
if [ $? = 1 ]; then
echo -e "libc\tFAIL"
echo -e "\nYou do not have libc libraries."
exit 1
else
#report libc not found if findlib returns 1
echo -e "libc\tOK\n"
fi
echo -n make && make --version > /dev/null
if [ $? != 0 ]; then
echo "gnumake not found"
exit 0
fi
echo -e "\tOK\n\nDONE"
case $1 in
"")
echo 'CC = gcc' > ./makefile
echo 'CFLAGS = -W xcet.c -o ./binaries/Linux/xcet' >> ./makefile
echo "" >> ./makefile
echo "xcet : xcet.c" >> ./makefile
echo -e "\t\$(CC) \$(CFLAGS)" >> ./makefile
echo "install : installer" >> ./makefile
echo -e '\t./installer' >> ./makefile
;;
#default makefile configurations, formated to proper makefile syntax. Modify CC and CFLAG lines for compiler and options, and change the rule "xcet" and the corresponding source file for your specific project
"--path")
echo 'CC = gcc' > ./makefile
echo "CFLAGS = -W xcet.c -o ./binaries/Linux/xcet" >> ./makefile
echo "" >> ./makefile
echo "xcet : xcet.c" >> ./makefile
echo -e "\t\$(CC) \$(CFLAGS)" >> ./makefile
echo "install : installer" >> ./makefile
echo -e "\t./installer --path $2" >> ./makefile
;;
#writes the makefile in the same way, but invokes installer with custom path arguments
#at this point everything else is custom to my program
"--factors")
if [ -z $2 ] || [ -z $3 ] 2> /dev/null; then
echo "./config --factors bfvar_factor pblen_factor"
exit 0
fi
mv ./xcet.h ./xcet.h.tmp
cat ./xcet.h.tmp | sed "s/bfvar_factor = [0-9]*/bfvar_factor = $2/g" > ./xcet.h.tmp2
cat ./xcet.h.tmp2 | sed "s/pblen_factor = [0-9]*/pblen_factor = $3/g" > ./xcet.h
rm ./xcet.h.tmp
rm ./xcet.h.tmp2
esac To test for gcc and make, I simply use the version parameters for those programs, and test the exit code. To see if libc6 is installed, I use "whereis" and then operate on the results with the findlib() function. The makefile is then written based on options given to the user defined by the case switch.
I'm not sure if this is the proper way of doing this, but it worked out nicely for me, especially because my program is simple enough not to require a really complex makefile. I suppose the most you'll need to do is edit the CC and CFLAGS to use your compiler with the options you need and the files, of course; after that, configuring the install script to match your package should be trivial as well. I'm actually a pretty novice programmer just getting back into it, so I don't really know the nuances involved with compiling a program with two source files, but I'm pretty sure this will work for you with the right CFLAG settnigs. Edit: I think you'll also need to add two source files under one rule, or otherwise modify the written makefile accordingly. It shouldn't be too hard to do that for a simple program.
The other problem is that, I can't really help with testing for those libraries. Another member here clued me into the "whereis" command, and it works if you do "whereis libc", but not glib. So, I'm not quite sure how those other libraries you're using function, but on my system "whereis gtk" comes back with /etc/gtk/. It's probablly just a matter of testing the correct library name, but "whereis gdk" produces nothing, which is most likely just because I don't have the gnome development kit installed. You might want to write a small bash script using the findlib function and see if it works for finding the libraries you need; if it works on one distribution, it should be able to find the locations on another too.
Now, aside from the config script, I have the installer script referred to by the makefile that is written by the config script.
Code:
#!/bin/bash
if [ $UID != '0' ]; then
echo -e "*WARNING*\n\nYou are not root, problems will probably occur\n\nIf you are using sudo this is probably fine just hit enter, otherwise enter 'q' and enter to quit and start as root\n\n"
read var
if [ $var = 'q' ] 2> /dev/null; then
exit 0
fi
fi
#test to see if user is root, if they are not, warn them that installation might not be successful, and give them the option to quit and start over
echo -e "\n\nInstalling...\n"
APPNAME=xcet
case $1 in
"")
PTH=/usr/bin
echo $PATH | grep -o /usr/bin 1> /dev/null
if [ $? != 0 ]; then
echo -e "/usr/bin not in path, use ./config --path and one of the following\n\n $PATH"
exit 0
fi
#test if PTH is in the users PATH
mv ./binaries/Linux/xcet $PTH/$APPNAME 2> /dev/null
#move the compiled binary to the PTH
if ! ls -lah $PTH/$APPNAME 2> /dev/null; then
echo "Unsuccessful"
if [ $UID != 0 ]; then
echo "This is a probably a permission issue, you are not root."
fi
exit 0
else
echo "xcet installed"
fi
#test for success, and if unsuccessful tests if problem is lack of permissions
;;
"--path")
PTH=$2
echo $PATH | grep -o $PTH 1> /dev/null
if [ $? != 0 ]; then
echo -e "$PTH not in path\n\n Use one of these\n\n $PATH"
exit 0
fi
mv ./binaries/Linux/xcet $PTH/$APPNAME 2> /dev/null
if ! ls -lah $PTH/$APPNAME 2> /dev/null; then
echo "Unsuccessful"
if [ $UID != 0 ]; then
echo "This is a probably a permission issue, you are not root."
exit 0
fi
else
echo "xcet installed"
fi
;;
esac
echo ""
$PTH/$APPNAME -a 2> /dev/null
if [ $? != '0' ]; then
if [ -e ./makefile ]; then
echo "XCET was installed but does not run properly. If you compiled, this should not happen, contact author."
else
echo "XCET was installed but does not run proplery. If you used the precompiled version, try compiling."
fi
fi As you can see, I tried to write the config and install script in a way that I might accomdate it for other programs in the future, so maybe it will help you out too. If you want to look at the whole packages to get a better idea of how it works in relation of the files and the aesthetic versus functional parts of the config/install scripts, you can download my program here
http://www.mousebaked.org/xcet.tar.gz
Now, I should let it be known that I haven't really tested this on a lot of distributions. Just Kubuntu, Debian Etch, and
Linux From Scratch I don't think that it would make any difference so long as "whereis" is installed, which I think is a standard on Linux systems, because it was present on Linux From Scratch.
Anyway, I hope this helps. I spent about three or four days working on this config and installer script combo, but I think the majority of that was just trying to figure out how it will work and how to do it. I think that the scripting is probably pretty obvious, but if you have any questions about what is going on and where, let me know by PM or here; I've got this thread subscribed to. I've also added comments to the scripts in this post to help; they don't have the same comments on the actual package though, so use this post to refer to them.