Advertisement
cheako

newdkms.pl

Sep 26th, 2015
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.45 KB | None | 0 0
  1. #!/usr/bin/env perl
  2. #
  3. # newdkms
  4. # Copyright (C) 2015 Michael Mestnik
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18. #
  19.  
  20. use common::sense;
  21. use Getopt::Std;
  22. use File::Path qw(make_path remove_tree);
  23. use File::Copy;
  24.  
  25. our($opt_n, $opt_v, $opt_s, $opt_H, $opt_l);
  26. getopts('n:v:sH:l:');
  27.  
  28. $opt_n && $opt_v || die 'n and v requiered.';
  29. my $p = ($opt_s ? '/usr' : '~') . "/src/${opt_n}-${opt_v}";
  30. make_path("$p/patches") >= 0 || die "make_path: Failed to create $p: $!";
  31.  
  32. my $optl = $opt_l ? "/kernel/$opt_l" : "/kernel/drivers/$opt_n";
  33. my $o;
  34. open($o, ">$p/dkms.conf") or die "Can't open dkms.conf: $!\n";
  35. print $o <<here;
  36. PACKAGE_NAME="$opt_n"
  37. PACKAGE_VERSION="$opt_v"
  38. BUILT_MODULE_NAME[0]="$opt_n"
  39. DEST_MODULE_LOCATION[0]="$optl"
  40. here
  41. close $o;
  42.  
  43. open($o, ">$p/${opt_n}.c") or die "Can't open ${opt_n}.c: $!\n";
  44. print $o <<'here';
  45. /*
  46.  *
  47. here
  48. if($opt_H){
  49.     open my $fh, '<', \$opt_H or die $!;
  50.     print $o qq{ *${("\n"," $_")[$_ ne '']}}
  51.         while (<$fh>);
  52.     close $fh;
  53. } else { print $o " * \n"; }
  54. print $o <<'here';
  55.  *
  56.  */
  57.  
  58. here
  59. close $o;
  60.  
  61. copy("$p/${opt_n}.c","$p/${opt_n}.h") or die "Can't copy ${opt_n}.c: $!\n";
  62.  
  63. my $_optn = $opt_n;
  64. $_optn =~ y/-/_/s;
  65. my $u_optn = uc$_optn;
  66. open($o, ">>$p/${opt_n}.h") or die "Can't open ${opt_n}.h: $!\n";
  67. print $o qq{#define ${u_optn}_VERSION "$opt_v"\n\n};
  68. close $o;
  69.  
  70. open($o, ">>$p/${opt_n}.c") or die "Can't reopen ${opt_n}.c: $!\n";
  71. print $o <<here;
  72. #include <linux/kernel.h>
  73. #include <linux/module.h>
  74. #include <linux/slab.h>
  75. #include <linux/init.h>
  76. #include <linux/types.h>
  77.  
  78. #include "${opt_n}.h"
  79.  
  80. MODULE_AUTHOR();
  81. MODULE_DESCRIPTION();
  82. MODULE_LICENSE();
  83. MODULE_VERSION(${u_optn}_VERSION);
  84.  
  85. // static ...
  86. // module_param(..., uint, 0);
  87. // MODULE_PARM_DESC(..., "");
  88.  
  89. static void ${_optn}_exit(void)
  90. {
  91.  
  92.         return;
  93. }
  94.  
  95. static void ${_optn}_init(void)
  96. {
  97.  
  98.         return;
  99. }
  100.  
  101. module_init(${_optn}_init);
  102. module_exit(${_optn}_exit);
  103. here
  104. close $o;
  105.  
  106. open($o, ">$p/Kconfig") or die "Can't open ${opt_n}.c: $!\n";
  107. print $o <<here;
  108. #
  109. # $opt_n $opt_v
  110. #
  111.  
  112. config ${u_optn}
  113.         tristate "$opt_n $opt_v"
  114. #       depends on
  115. #       select
  116.         ---help---
  117.           Some inspiering text.
  118.  
  119. here
  120. close $o;
  121.  
  122. open($o, ">$p/Makefile") or die "Can't open ${opt_n}.c: $!\n";
  123. print $o <<here;
  124. obj-m := ${opt_n}.o
  125.  
  126. KVER  ?= \$(shell uname -r)
  127.  
  128. KDIR ?= /lib/modules/\$(KVER)/build
  129. MDIR ?= /lib/modules/\$(KVER)$optl
  130.  
  131. PWD := \$(shell pwd)
  132.  
  133. default:
  134.         \$(MAKE) -C \$(KDIR) SUBDIRS=\$(PWD) \$(obj-m:.o=.ko)
  135.  
  136. install:
  137.         install -d \$(MDIR)
  138.         install -m 644 -c \$(obj-m:.o=.ko) \$(MDIR)
  139.         depmod -a
  140.  
  141. clean:
  142.         \$(MAKE) -C \$(KDIR) SUBDIRS=\$(PWD) \$(obj-m:.o=.ko) clean
  143.         rm -rf *.mod.c *.o *.ko .tmp_versions Module.symvers .\$(obj-m:.o=)*
  144.         rm -rf linux/.*o.cmd linux/*.o
  145.  
  146. here
  147. close $o;
  148.  
  149. exit;
  150.  
  151. 1;
  152. __END__
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement