Class: Platform

Inherits:
Object
  • Object
show all
Defined in:
lib/makeconf/platform.rb

Overview

Abstraction for platform-specific system commands and variables This class only contains static methods.

Constant Summary collapse

@@host_os =

TODO: allow target_os to be overridden for cross-compiling

RbConfig::CONFIG['host_os']
@@target_os =
@@host_os

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#host_osObject (readonly)

Returns the value of attribute host_os.



7
8
9
# File 'lib/makeconf/platform.rb', line 7

def host_os
  @host_os
end

#target_osObject (readonly)

Returns the value of attribute target_os.



7
8
9
# File 'lib/makeconf/platform.rb', line 7

def target_os
  @target_os
end

Class Method Details

.archiver(archive, members) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/makeconf/platform.rb', line 59

def Platform.archiver(archive,members)
  if is_windows? && ! ENV['MSYSTEM']
    'lib.exe ' + members.join(' ') + ' /OUT:' + archive
  else
    # TODO: add '/usr/bin/strip --strip-unneeded' + archive
    'ar rs ' + archive + ' ' + members.join(' ')
  end
end

.cp(src, dst) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/makeconf/platform.rb', line 98

def Platform.cp(src,dst)
  if src.kind_of?(Array)
    src = src.join(' ')
  end

  if is_windows? && ! ENV['MSYSTEM']
    return "copy #{src} #{dst}"
  else
    return "cp #{src} #{dst}"
  end
end

.dev_nullObject

Send all output to /dev/null or it’s equivalent



111
112
113
114
115
116
117
# File 'lib/makeconf/platform.rb', line 111

def Platform.dev_null
  if is_windows? && ! ENV['MSYSTEM'] 
    ' >NUL 2>NUL' 
  else
    ' >/dev/null 2>&1'
  end
end

.dev_null_stderrObject

Send standard error to /dev/null or it’s equivalent



120
121
122
123
124
125
126
# File 'lib/makeconf/platform.rb', line 120

def Platform.dev_null_stderr
  if is_windows? && ! ENV['MSYSTEM'] 
    ' 2>NUL' 
  else
    ' 2>/dev/null'
  end
end

.executable_extensionObject

The extension used for executable files



129
130
131
# File 'lib/makeconf/platform.rb', line 129

def Platform.executable_extension
  is_windows? ? '.exe' : ''
end

.execute(cmd) ⇒ Object

Run an external command. On Windows, the system() function uses cmd.exe which pops up an ugly DOS window.



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/makeconf/platform.rb', line 176

def Platform.execute(cmd)
  if is_windows?
    # NEED INFO
      #shell = WIN32OLE.new('Shell.Application')
#shell.ShellExecute('cmd.exe', '', ,,

# DOESNOT WORK
#p = IO.popen(cmd)
#        p.readlines
#        return $?.exitstatus == 0 ? true : false


      
      # NOTE: requires Ruby 1.9
#    pid = Process.spawn(cmd)
#        pid, status = Process.waitpid2(pid)
#        return status.exitstatus == 0 ? true : false

      return system(cmd)
  else
      system(cmd)
  end
end

.is_graphical?Boolean

Returns true if the current environment supports graphical display

Returns:

  • (Boolean)


149
150
151
152
153
# File 'lib/makeconf/platform.rb', line 149

def Platform.is_graphical?
  return true if Platform.is_windows?
  return true if ENV.has_key?('DISPLAY') and not ENV['DISPLAY'].empty?
  return false
end

.is_linux?Boolean

Returns true or false depending on if the target is Linux

Returns:

  • (Boolean)


27
28
29
# File 'lib/makeconf/platform.rb', line 27

def Platform.is_linux?
  @@target_os =~ /^linux/
end

.is_solaris?Boolean

Returns true or false depending on if the target is Solaris

Returns:

  • (Boolean)


22
23
24
# File 'lib/makeconf/platform.rb', line 22

def Platform.is_solaris?
  @@target_os =~ /^solaris/
end

.is_superuser?Boolean

Returns true if the user is running as the superuser on Unix or has Administrator privileges on Windows.

Returns:

  • (Boolean)


202
203
204
205
206
207
208
# File 'lib/makeconf/platform.rb', line 202

def Platform.is_superuser?
  if is_windows?
    Process.euid == 0
  else
    system "reg query HKU\\S-1-5-19" + Platform.dev_null
  end
end

.is_windows?Boolean

Returns true or false depending on if the target is MS Windows

Returns:

  • (Boolean)


17
18
19
# File 'lib/makeconf/platform.rb', line 17

def Platform.is_windows?
  @@target_os =~ /mswin|mingw/
end

.is_x86?Boolean

Returns true or false depending on if the target is x86-compatible

Returns:

  • (Boolean)


32
33
34
# File 'lib/makeconf/platform.rb', line 32

def Platform.is_x86?
  RbConfig::CONFIG['host_cpu'] =~ /^(x86_64|i386)$/ ? true : false
end

.mkdir(path) ⇒ Object

Create a directory and all of it’s components



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/makeconf/platform.rb', line 69

def Platform.mkdir(path)
  if path.kind_of?(Array)
      path = path.flatten.join(' ')
  end
  throw 'invalid path' if path.nil? or path == ''
  if is_windows?
     "mkdir '#{path}'"
  else
     "umask 22 ; mkdir -p '#{path}'"
  end
end

.object_extensionObject

The extension used for intermediate object files



134
135
136
# File 'lib/makeconf/platform.rb', line 134

def Platform.object_extension
  is_windows? ? '.obj' : '.o'
end

.pathspec(path) ⇒ Object

Converts a slash-delimited path into a backslash-delimited path on Windows.



166
167
168
169
170
171
172
# File 'lib/makeconf/platform.rb', line 166

def Platform.pathspec(path)
  if is_windows?
    path.gsub(%r{/}) { "\\" }
  else
    path
  end
end

.rm(path) ⇒ Object

Remove a regular file



82
83
84
85
86
87
88
89
90
91
# File 'lib/makeconf/platform.rb', line 82

def Platform.rm(path)
  if path.kind_of?(Array)
      path = path.join(' ')
  end
  if is_windows? && ! ENV['MSYSTEM']
    return 'del /F ' + path
  else
    return 'rm -f ' + path
  end
end

.rmdir(path) ⇒ Object

Remove a directory along with all of it’s contents



94
95
96
# File 'lib/makeconf/platform.rb', line 94

def Platform.rmdir(path)
  is_windows? ? "rmdir /S /Q #{path}" : "rm -rf #{path}"
end

.shared_library_extensionObject

The extension used for shared libraries



144
145
146
# File 'lib/makeconf/platform.rb', line 144

def Platform.shared_library_extension
  is_windows? ? '.dll' : '.so'
end

.static_library_extensionObject

The extension used for static libraries



139
140
141
# File 'lib/makeconf/platform.rb', line 139

def Platform.static_library_extension
  is_windows? ? '.lib' : '.a'
end

.vendorObject

Returns the name of the operating system vendor



37
38
39
40
41
42
# File 'lib/makeconf/platform.rb', line 37

def Platform.vendor
  return 'Fedora' if File.exists?('/etc/fedora-release')
  return 'Red Hat' if File.exists?('/etc/redhat-release')
  return 'Debian' if File.exists?('/etc/debian_version')
  return 'Unknown' 
end

.which(command) ⇒ Object

Emulate the which(1) command



156
157
158
159
160
161
162
163
# File 'lib/makeconf/platform.rb', line 156

def Platform.which(command)
  return nil if is_windows?      # FIXME: STUB
  ENV['PATH'].split(':').each do |prefix|
    path = prefix + '/' + command
    return command if File.executable?(path)
  end
  nil
end

.word_sizeObject

Returns the native word size



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/makeconf/platform.rb', line 45

def Platform.word_size
  if @@host_os =~ /^solaris/
    `/usr/bin/isainfo -b`.to_i
  elsif @@host_os =~ /^linux/
    if `/usr/bin/file /bin/bash` =~ /32-bit/
      return 32
    else
      return 64
    end
  else
    throw 'Unknown word size'
  end
end